2

我有一个使用 GlazedLists 添加预先输入功能的 JComboBox。我希望用户输入一个字符串并查看预输入(这要归功于 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));       
     }
});
4

1 回答 1

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) {
            }
        };
    }
});
于 2014-02-04T10:08:41.163 回答