JcomboBox 多选
我的应用程序中有一个 jcombobox。 我希望它支持多重选择(就像在 jlist 中一样)。 有代码示例吗?
I have a jcombobox in my application.
and i want it to support multiple selection (like it do in a jlist).
is there any code example?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这是不可能的,除非你使用 JList,就像你说的那样。
JComboBox API 报告:
还有一个 JComboBox 教程:
更新:
我查看了这个答案,因为实际上“可能”使用 ListCellRenderer 来做到这一点,为每个项目添加一个复选框。请考虑这个答案来实施这个“解决方案”。
但是,我认为这不是一个好主意,原因如下:
I think it's not possible, unless you use a JList, like you said.
The JComboBox API reports:
And a JComboBox tutorial:
Update:
I reviewed this answer, because actually it's "possible" to do that using a ListCellRenderer, adding a checkbox to each item. Please consider this answer to implement this "solution".
However, I don't think it's a good idea for the following reasons:
这并非完全不可能,但要完成这项工作还有很多工作要做。您需要创建自己的类来扩展/实现所有这些:
And you'll need to develop your own class that implements ItemListener, MouseListener, PopupMenuListener, MouseMotionListener, PropertyChangeListener, KeyListener, ListSelectionListener.
Hint: you'll need to override a lot of createXXXListener() methods in the UI classes in order to bypass the many places where multiple selections are discarded.
[And it still doesn't allow for an editable multi-selection combo.]
It's not quite impossible but there's a lot of work to do to get the job done. You'll need to create your own classes to extend/implement all of these:
And you'll need to develop your own class that implements ItemListener, MouseListener, PopupMenuListener, MouseMotionListener, PropertyChangeListener, KeyListener, ListSelectionListener.
Hint: you'll need to override a lot of createXXXListener() methods in the UI classes in order to bypass the many places where multiple selections are discarded.
[And it still doesn't allow for an editable multi-selection combo.]