Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide JComBox Box Arrow

Is it possible to hide the arrow displayed in the JComboBox

I tried setting:

combo.getComponent(0).setSize(new Dimension(1,1));

But it doesnt seem to work

like image 224
Akash Avatar asked Sep 20 '11 13:09

Akash


People also ask

Can We disable jcombobox arrow button in Java?

Can we disable JComboBox arrow button in Java? Yes, we can do that using removeArrow () method. The following is an example to disable JComboBox arrow button:

How do I hide the drop-down arrow on a combo box?

Steps to hide the drop-down arrow on a combo box To hide the drop-down arrow on a combo box when the combo box is not selected, follow these steps: Start Access. On the Helpmenu, point to Sample Databases, and then click Northwind Sample Access Database.

How can I make combo boxes read-only?

Three possible approaches: 1. Carefully place an empty label control over each arrow, setting its BackColor property to match the section's BackColor, and its BackStyle property to Normal. Set the Locked property of each combo box to True and its Enabled property to False to make them read-only. 2.

How do I create an employee ID combo box in designview?

On the Helpmenu, point to Sample Databases, and then click Northwind Sample Access Database. Open the Orders form in Designview. Add a rectangle control to the form. Size and move the rectangle control so that it completely covers the drop-down arrow on the EmployeeID combo box.


2 Answers

You have to create a new combobox UI for that:

combo.setUI(new BasicComboBoxUI() {
    protected JButton createArrowButton() {
        return new JButton() {
            public int getWidth() {
                return 0;
            }
        };
    }
});

But be careful to inherited from the base UI which matches your current look and feel.

For example if you are using Substance you should derive your new UI from SubstanceComboBoxUI instead of BasicComboBoxUI. Otherwise you'll might loose features provided by your current L&F.

EDIT: If you want this to get some kind of auto-completion feature it's better to stick with a normal JTextField and use AutoCompleteDecorator from SwingX.

like image 51
Daniel Rikowski Avatar answered Nov 20 '22 03:11

Daniel Rikowski


I've been looking for a solution to this for a while now, and it turns out that all it really takes is remembering that JComboBox is a compound component.

for (Component component : TheComboBox.getComponents())
{
    if (component instanceof JButton) {
        TheComboBox.remove(component);
    }
}

Thanks go to mKorbel for the reminder.

like image 20
Morgen Avatar answered Nov 20 '22 03:11

Morgen