java 如何使用多选创建组合框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9992003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create Combobox with multiselection?
提问by manhnt
I need to create a combo box with multi-selection, how to achieve that?
我需要创建一个多选组合框,如何实现?
采纳答案by Mikle Garin
There are a few basic problems with creating custom combobox popup content (like a list with multiselection):
1.Default UI suggests JList usage as the content so to change that behavior you will have to change the whole ComboBoxUI
2.You cannot simply change the default combobox list into multiselection one due to the fact that only one value gets "selected" at the end and list has default rollover selection mouse listener, that will make you unable to choose more than one element
创建自定义组合框弹出内容(例如带有多选的列表)存在一些基本问题:
1.默认 UI 建议使用 JList 作为内容,因此要更改该行为,您必须更改整个 ComboBoxUI
2.您不能简单地更改由于最后只有一个值被“选择”并且列表具有默认的鼠标悬停选择侦听器,因此默认组合框列表变为多选一,这将使您无法选择多个元素
So i'd reccomend you to use simple JList instead of combobox or look into using some extended components libraries like JideSoft- they have this component and lots more which you won't be able to quickly create using Swing features.
所以我建议你使用简单的 JList 而不是组合框,或者考虑使用一些扩展的组件库,比如JideSoft- 他们有这个组件以及更多你将无法使用 Swing 功能快速创建的组件。
回答by Alex Kartishev
I know, that the question is rather old, but for those, who still looks for solution of this problem, try the following code:
我知道,这个问题已经很老了,但是对于那些仍在寻找此问题解决方案的人,请尝试以下代码:
public class ComboSelections {
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, UnsupportedLookAndFeelException {
UIManager.setLookAndFeel((LookAndFeel) Class.forName("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel").newInstance());
final JPopupMenu menu = new JPopupMenu();
JMenuItem one = new JCheckBoxMenuItem("One");
JMenuItem two = new JCheckBoxMenuItem("Two");
JMenuItem three = new JCheckBoxMenuItem("Three");
JMenuItem four = new JCheckBoxMenuItem("Four");
menu.add(one);
menu.add(two);
menu.add(three);
menu.add(four);
final JButton button = new JButton("Click me");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!menu.isVisible()) {
Point p = button.getLocationOnScreen();
menu.setInvoker(button);
menu.setLocation((int) p.getX(),
(int) p.getY() + button.getHeight());
menu.setVisible(true);
} else {
menu.setVisible(false);
}
}
});
one.addActionListener(new OpenAction(menu, button));
two.addActionListener(new OpenAction(menu, button));
three.addActionListener(new OpenAction(menu, button));
four.addActionListener(new OpenAction(menu, button));
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.add(button);
frame.getContentPane().add(panel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private static class OpenAction implements ActionListener {
private JPopupMenu menu;
private JButton button;
private OpenAction(JPopupMenu menu, JButton button) {
this.menu = menu;
this.button = button;
}
@Override
public void actionPerformed(ActionEvent e) {
menu.show(button, 0, button.getHeight());
}
}
}