我有一个包含对象的数组列表和一个正在运行的 Gui。我正在寻找一种方法来弹出一个小框架或框或类似的东西来显示数组列表中的对象。用户现在应该能够选择一个或多个项目,然后将其返回。
我已经有了选项面板,但我只能选择一个对象
Object[] possibilities = lr.declarationList.toArray();
String s = (String)JOptionPane.showInputDialog(
gui.getFrame(),
"Choose Target Nodes",
"Customized Dialog",
JOptionPane.PLAIN_MESSAGE,
null,
possibilities,
null);
也许弹出列表会有所帮助。
最佳答案
尝试使用 JOptionPane.showMessageDialog(...)
带有 JList 组件参数,其元素来自您的列表,例如:
JList list = new JList(new String[] {"foo", "bar", "gah"});
JOptionPane.showMessageDialog(
null, list, "Multi-Select Example", JOptionPane.PLAIN_MESSAGE);
System.out.println(Arrays.toString(list.getSelectedIndices()));
请注意,如果消息对象本身需要更多布局项,您可以将它们全部打包到 JPanel 中并将该组件用作消息参数。
关于java - JOptionPane 的多项选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8899605/