Swing 相当于 HTML
我希望我的 JComboBox 将多个选项组合在一起,类似于 HTML optgroup
< /a>:
<select>
<optgroup label="A">
<option/>
<option/>
</optgroup>
</select>
我在 Swing 中找不到任何解决方案。操纵 Combobox 的 UI 渲染器似乎是一个坏主意,因为它的操作系统和界面都不同。依赖于 L&F(并且它们是私有的,因此无法扩展)。
I want my JComboBox to group multiple options together, similar to the HTML optgroup
:
<select>
<optgroup label="A">
<option/>
<option/>
</optgroup>
</select>
I could not find any solution for this in Swing. Manipulating the UI-Renderer for the Combobox seems to be a bad idea, as it's OS & L&F-dependent (and they are private so cannot extend).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
考虑以下实现作为如何应用自定义样式和创建不可选择项的基本指南:
Consider the following implementation as a basic guide how to apply custom styling and create non-selectable items:
您可以在自定义渲染器中执行此操作,如 中所述如何使用组合框:提供自定义渲染器。
You can do this in a custom renderer, as discussed in How to Use Combo Boxes: Providing a Custom Renderer.
我不相信有一种简单的方法可以做到这一点,但有一种方法可以做到这一点。
我将实现一个数据模型类来指示您上面描述的分组。将这些数据模型的实例放入 javax.swing.ComboBoxModel 实现实例。
然后,您可以实现 javax.swing.ListCellRenderer 根据需要设置文本数据缩进的输出格式。您可能只想扩展 javax.swing.DefaultListCellRenderer 或者可能从 Java 源代码借用它的实现。
至于 L&F,您应该能够通过使用上述方法保持在正常指导范围内,并且您不必费力去弄清楚如何实施它。查看默认的 Swing 组件,它们将提供有关如何处理 L&F 的大量见解。
此外,我认为有一些机制(您必须原谅我,自从我完成完整的 Swing 开发以来已经很多年了)可以让您确定某个项目是否可选。
I don't believe that there is one simple way of doing this, but there is a way to do it.
I would implement a data model class that indicates the grouping that you've describe above. Place instances of those data models in your javax.swing.ComboBoxModel implementation instance.
You can then implement a javax.swing.ListCellRenderer to format the output as you like with indents for the text data. You may just want to extend the javax.swing.DefaultListCellRenderer or possibly borrow its implementation wholesale from the Java source.
As for the L&F you should be able to stay within normal guidelines by using the above methods and you won't have to fight with figuring out how to implement it. Look at the default Swing components they will provide a lot of insight in to how to deal with L&F.
Additionally, I think there are mechanisms (you'll have to forgive me, it's been YEARS since I've done full Swing development) to allow you to determine if an item is selectable or not.
今天我自己就想要这个,并且我花了一天的时间来弄清楚如何将这些东西拼凑在一起,以使用 JList 而不是使用建议的 JComboBox 来实现类似的模型。我终于想出了一个使用 GlazedLists EventList 和 SeparatorList 以及相应的 DefaultEventListModel 的解决方案。我重写了 CellRenderer 和 DefaultListSelectionModel。最后,我对自己的问题发布了自己的答案:如何防止在 JList 中选择 SeparatorList.Separator?
这是我的最终工作代码:
I was wanting this myself today, and I've spent the day figuring it out piecing things together to implement a similar model with a JList rather than with the suggested JComboBox. I've finally come up with a solution using GlazedLists EventList and SeparatorList with the corresponding DefaultEventListModel. I override the CellRenderer and the DefaultListSelectionModel. In the end I posted my own answer to my own question on this: How to prevent selection of SeparatorList.Separator in a JList?
Here's my final working code: