JcomboBox 多选

发布于 2024-12-20 18:11:59 字数 63 浏览 0 评论 0原文

我的应用程序中有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠栀 2024-12-27 18:11:59

我认为这是不可能的,除非你使用 JList,就像你说的那样。

JComboBox API 报告:

用户可以从下拉列表中选择一个值,该值显示在
用户的请求。

还有一个 JComboBox 教程

列表并不是很有吸引力,但它们比
当项目数量很大(例如超过 20 个)或当
选择多个项目可能是有效的。

更新:

我查看了这个答案,因为实际上“可能”使用 ListCellRenderer 来做到这一点,为每个项目添加一个复选框。请考虑这个答案来实施这个“解决方案”。

但是,我认为这不是一个好主意,原因如下:

  1. 有一个像 JList 这样允许多重选择的控件;
  2. JComboBox 控件专为单个项目选择而设计;
  3. 对我来说,仍然使用 JComboBox 控件并允许多重选择是没有意义的。

I think it's not possible, unless you use a JList, like you said.

The JComboBox API reports:

The user can select a value from the drop-down list, which appears at
the user's request.

And a JComboBox tutorial:

Lists are not terribly attractive, but they're more appropriate than
combo boxes when the number of items is large (say, over 20) or when
selecting multiple items might be valid.

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:

  1. there's a control like a JList that allows multiple selection;
  2. a JComboBox control is designed just for single item selection;
  3. it doesn't make sense to me to still use a JComboBox control and allow a multiple selection.
深巷少女 2024-12-27 18:11:59

这并非完全不可能,但要完成这项工作还有很多工作要做。您需要创建自己的类来扩展/实现所有这些:

  • ListCellRenderer(这样您就可以在弹出窗口显示时标记所选项目,并在未显示弹出窗口时指示(至少)有多个选择)。
  • JComboBox(显然)
  • BasicComboBoxUI(这实际上是大部分工作所在)
  • BasicListUI(至少我必须这样做)
  • ComboBoxModel

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:

  • ListCellRenderer (so you can mark the selected items when the popup is showing and indicate (at least) that there are multiple selections when it is not).
  • JComboBox (obviously)
  • BasicComboBoxUI (this is actually where most of the work is)
  • BasicListUI (at least I had to)
  • ComboBoxModel

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.]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文