隐藏ComBoBox箭头

时间:2011-09-20 13:20:53

标签: java swing jcombobox

是否可以隐藏JComboBox中显示的箭头

我尝试过设置:

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

但它似乎无法正常工作

2 个答案:

答案 0 :(得分:8)

您必须为此创建一个新的组合框UI:

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

但要小心从基本UI继承,这与您当前的外观相符。

例如,如果您使用的是物质,则应从SubstanceComboBoxUI而不是BasicComboBoxUI派生新的用户界面。否则,您可能会失去当前L& F。

提供的功能

编辑:如果您希望获得某种自动完成功能,最好坚持正常JTextField并使用SwingX中的AutoCompleteDecorator

答案 1 :(得分:3)

我一直在寻找解决方案一段时间了,事实证明,真正需要的是记住JComboBox是一个复合组件。

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

感谢mKorbelreminder

相关问题