How do you remove the JComboBox 'click and see dropdown' functionality?

纵饮孤独 提交于 2019-12-11 02:38:38

问题


I have a JComboBox which uses GlazedLists to add type-ahead functionality. I want the user to type in a string and see type-ahead (this is working thanks to Glazedlists). However, I don't want users to be able to click the down arrow of the combobox and inspect the dropdown list. I've made the down arrow invisible and made the combobox editable so that it resembles a JTextField. However, you are still able to hover the mouse over the area where the down arrow used to be and click it. This results in the dropdown appearing. What do I change or which methods do I override in order to remove the 'click and get dropdown' functionality.

ComboBox<String> box = new ComboBox<String>();
box.setEditable(true);
box.setUI(new BasicComboBoxUI(){ // make the down arrow invisible
    protected JButton createArrowButton() {
       return new JButton() {
           public int getWidth() {
               return 0;
           }
       };
    }
});

SwingUtilities.invokeLater(new Runnable(){
     public void run(){
         Object[] elements = new Object[] {"java", "perl", "python", "haskell", "erlang", "groovy"};
         AutoCompleteSupport.install(box, GlazedLists.eventListOf(elements));       
     }
});

回答1:


Override JButton#addMouseListener:

JComboBox<String> box = new JComboBox<>();
box.setEditable(true);
box.setUI(new BasicComboBoxUI() { // make the down arrow invisible
    protected JButton createArrowButton() {
        return new JButton() {
            public int getWidth() {
                return 0;
            }

            @Override
            public synchronized void addMouseListener(MouseListener l) {
            }
        };
    }
});


来源:https://stackoverflow.com/questions/21453354/how-do-you-remove-the-jcombobox-click-and-see-dropdown-functionality

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