Hide JComBox Box Arrow

南楼画角 提交于 2019-12-08 19:16:40

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/7485832/hide-jcombox-box-arrow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!