如何删除JComboBox的“点击并查看下拉列表”功能?

时间:2014-01-30 10:07:24

标签: java jcombobox typeahead glazedlists

我有一个JComboBox,它使用GlazedLists来添加预先输入功能。我希望用户输入一个字符串并查看预先输入(这要归功于Glazedlists)。但是,我不希望用户能够单击组合框的向下箭头并检查下拉列表。我使向下箭头不可见,并使组合框可编辑,使其类似于JTextField。但是,您仍然可以将鼠标悬停在以前使用向下箭头的区域上并单击它。这导致下拉出现。我要更改哪些方法或覆盖哪些方法以删除“点击并获取下拉”功能。

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 个答案:

答案 0 :(得分:2)

覆盖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) {
            }
        };
    }
});