swing JTree、JTable 或 JList 中的活动元素
我需要一个具有类似 JPanel
的容器,一个在另一个下面排列,可以选择。我可以:
- 将
JList
与自定义渲染器一起使用,但那些JPanel
将是被动元素,这不是我想要的。 - 使用“活动”
JPanel
实现我自己的容器,但这些容器不可选。或者他们可以选择吗? 也许MouseListener
和访问系统默认的selected-background-colors可能是一种方法,但似乎有点费力 - 使用
JTable
或使用自定义单元格编辑器呈现“活动”JPanel
的JTree
。但这些活动部分只会在“第二次”单击时做出反应,首先激活编辑器,然后执行JPanel
的实际操作。这也是不可接受的。
为了获得更直观的印象,下面是一个示例来说明这可能意味着什么:
包含列表项的 JList
,每个列表项都有两个功能 JButton
。
I need a Container with similar JPanel
s lined up one below the other which can be selected. I could:
- Use a
JList
with a custom renderer but thoseJPanel
s would be passive elements, that's not what I want. - Implement my own Container with 'active'
JPanel
s but those would not be selectable. Or could they made selectable?
Maybe aMouseListener
and access to the system default selected-background-colors could be a way but it seems a bit too much effort - Use a
JTable
orJTree
with custom cell editors rendering the 'active'JPanel
. But these active parts would only react at the 'second' click, first to activate the editor, second to perform the real action of theJPanel
. this is also not acceptable.
To get a more visual impression, here is an example of what this could mean:
A JList
containing list items which have each two functional JButtons
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如您所发现的,简单地将
JPanel
放入JList
中并不能完全按照您的意愿工作。JPanel
将是被动的,不会接收事件 - 本质上所有发生的事情都是您的JPanel
只是被绘制,它不是一个活动组件。不要使用
JList
将面板放入列表中,而是使用类似列表的布局管理器,例如BoxLayout
或GridLayout
。如果您希望所有面板具有相同的大小,请使用仅包含单个列的GridLayout
。As you've discovered, simply putting a
JPanel
inside aJList
doesn't quite work as you'd like. TheJPanel
will be passive and won't receive events - essentially all that is happening is yourJPanel
is simply being drawn, it's not a living component.Instead of using a
JList
to put your panels in a list, use a list-like layout manager, such asBoxLayout
orGridLayout
. If you want all your panels to be the same size, useGridLayout
with only a single column.我不确定我是否理解你的“例子”。如果您需要两个功能按钮,请使用 JTable,其中功能按钮包含在单独的列中。然后您的数据将显示在其他列中。
表格按钮列展示了如何执行此操作。
I'm not sure I understand your "example". If you want two functional buttons, then use a JTable where the functional buttons are contained is separate columns. Then your data would be displayed in other columns.
Table Button Column shows how you can do this.
您的问题对我来说不清楚,也许这里
有JTable,有一个
TableColumn
但没有TableHeader
,包含 JPanel 内有活动的JComponents
(您可以实现 TableCellEditor 对于所有 JComponent) 为 JComboBox, JButton 和 JTextFieldyour question(s) isn't clear for me, maybe here
there is JTable, with one
TableColumn
but withoutTableHeader
, contains JPanel with activeJComponents
inside (you can implements TableCellEditor for all JComponents) as JComboBox, JButton and JTextField