覆盖 Swing 中的列表选择
我想进行列表选择,这样当我第一次选择一个项目时,它就会被选中,然后当我选择第二个项目时,两个项目都应该被选中,第一个选择应该保持原样。我已将列表选择模式设置为多重选择。但仍然必须按ctrl
键才能完成该操作。 我想在不按 ctrl
键的情况下执行此操作。
如何保持列表项处于选中状态?
这是其中的行我设置选择模式:
list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
编辑: 正如StanislavL建议的那样,我尝试遵循,但它对我不起作用。
DefaultListSelectionModel model = new DefaultListSelectionModel();
model.removeSelectionInterval(0, 2);
user_list.setSelectionModel(model);
I want to make list selection such that when I first select an item it get selected after that when I select second item then both should get selected that is first selection should remain as it is. I have set list selection mode to multiple selection. But still has to press ctrl
key to do the thing. I want to do it without pressing ctrl
key.
How to remain list item selected?
Here is the line where I set the selection mode:
list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
Edit:
AS suggested by StanislavL I tried following but it did not worked for me.
DefaultListSelectionModel model = new DefaultListSelectionModel();
model.removeSelectionInterval(0, 2);
user_list.setSelectionModel(model);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个众所周知的约定,多选需要按Ctrl键。与使用 Shift 键选择值范围相同。
就我个人而言,我永远不会花精力去改变这种行为,因为它会让已经熟悉其他应用程序的用户感到困惑。
It is a real well known convention that multi select requires pressing the Ctrl key. Same with using the Shift key to select a range of values.
Personally I would never spend effort on changing this behaviour, because it would confuse users that are already familiar with other applications.
尝试替换列表中的ListSelectionModel。使用
您可以使用 DefaultListSelectionModel 并覆盖
调用 super.remove()(如果已选择项目)。
Try to replace ListSelectionModel in the list. Use
You can use the DefaultListSelectionModel and override
call super.remove() if the items are already selected.