【问题标题】:How do you remove the JComboBox 'click and see dropdown' functionality?如何删除 JComboBox 的“单击并查看下拉菜单”功能?
【发布时间】:2014-02-22 13:41:53
【问题描述】:

我有一个 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));       
     }
});

【问题讨论】:

    标签: java jcombobox typeahead glazedlists


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-07
      • 1970-01-01
      相关资源
      最近更新 更多