Java/Swing 中的分段列表?
我在 JList
中有一个项目列表供用户选择。 由于它有很多项目(例如,州中的城市),我想将列表分为几个部分。 不过,章节标题不应该是可选择的。 因此,对于我的城市/州示例,这可能如下所示:
- 州 1
- 城市1
- 城市 2
- 城市 3
- 州 2
- 城市 4
- 城市 5
- 城市 6
通过将 JList
嵌入自定义 ListCellRenderer
中,自己编写此代码并不困难,但我想知道是否那里已经有这样的课程了。
I have a list of items in a JList
for the user to select. Since it's a lot of items (say, cities in states), I want to divide the list into sections. The section headings should not be selectable, though. So for my cities/states example, this might look like this:
- State 1
- City 1
- City 2
- City 3
- State 2
- City 4
- City 5
- City 6
It wouldn't be so difficult to write this myself by embedding JList
s in a custom ListCellRenderer
, but I'm wondering if there already is a class like that out there.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 Apple 所谓的 SourceList。 您可以在 iTunes 和 Mac OS X 的 Finder 中看到它们的运行情况。 这是对您所描述的问题的优雅解决方案。
用于执行此操作的跨平台开源 Java Swing 组件如下:
http://explodingpixels.wordpress.com/2008/09 /08/mac-widgets-for-java/
You could use what Apple calls a SourceList. You see them in action in iTunes and in Mac OS X's Finder. It is an elegant solution to the problem you describe.
A cross-platform, open source Java Swing component for doing this is here:
http://explodingpixels.wordpress.com/2008/09/08/mac-widgets-for-java/
JIDE 提供了一个组件,可以让您做到这一点。 它称为 GroupList:
(来源:jidesoft.com)
There's a component available with JIDE that let's you do exactly this. It's called GroupList:
(source: jidesoft.com)
也许通过使用 JTree ? 你所描述的是一棵有两层的树。
Maybe by using JTree ? What you describe is a Tree with two levels.
我看到这个问题已经得到解答,但我注意到罗伯特评论说他希望有一个开源解决方案。 我建议使用 Glazed Lists 的分隔符列表,您可以在此处找到该 API:
http://publicobject.com/glazedlists/glazedlists-1.8.0/api/ca/odell/glazedlists/SeparatorList.html
以下是一些示例代码,将生成按首字母分组的项目列表:
alt text http://img300.imageshack.us /img300/8977/separatorlist.png
}
I see this question is already answered, but I noticed that Robert commented that he was hoping for an open source solution. I'd recommend using Glazed Lists' Separator list, the API for which you can be found here:
http://publicobject.com/glazedlists/glazedlists-1.8.0/api/ca/odell/glazedlists/SeparatorList.html
Here's some sample code that will produce a list of items grouped by their first letter:
alt text http://img300.imageshack.us/img300/8977/separatorlist.png
}