我有一个 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));
}
});
覆盖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) {
}
};
}
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)