JList - 选择多个项目
当我想自动选择 JList
中的多个值时,我在 JList
中遇到了这个 setSelectedValue()
方法的问题,它仍然只选择了一个。有办法吗?
String[] items = { "Item 1", "Item 2", "Item 3", "Item 4" };
final JList theList = new JList(items);
theList.setSelectedValue("Item 1",true);
theList.setSelectedValue("Item 2",true);
此代码仅显示选定的Item 2
。
I faced a problem with this setSelectedValue()
method in JList
when I wanted to select multiple values in a JList
automatically, it still selected only one. Is there a way?
String[] items = { "Item 1", "Item 2", "Item 3", "Item 4" };
final JList theList = new JList(items);
theList.setSelectedValue("Item 1",true);
theList.setSelectedValue("Item 2",true);
This code shows only Item 2
as selected.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
调用
JList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION)
后使用JList.setSelectedIndices(int[])
。EG
屏幕截图
Use
JList.setSelectedIndices(int[])
after callingJList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION)
.E.G.
Screen Shot
或者如果选择不连续,那么您需要使用多个
or if the selection isn't consecutive then you need to use multiple
当您使用 NetBeans GUI 编辑器时,您可以自定义为
JList
生成的创建后代码
,如下所示。As you are using the NetBeans GUI editor, you can customize the
Post-Creation Code
generated for yourJList
as shown below.