如何删除JComboBox的“单击并查看下拉"功能? [英] How do you remove the JComboBox 'click and see dropdown' functionality?

查看:115
本文介绍了如何删除JComboBox的“单击并查看下拉"功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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));       
     }
});

推荐答案

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

这篇关于如何删除JComboBox的“单击并查看下拉"功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆