swing JTree、JTable 或 JList 中的活动元素

发布于 2024-12-02 15:35:08 字数 580 浏览 0 评论 0原文

我需要一个具有类似 JPanel 的容器,一个在另一个下面排列,可以选择。我可以:

  1. JList 与自定义渲染器一起使用,但那些 JPanel 将是被动元素,这不是我想要的。
  2. 使用“活动”JPanel 实现我自己的容器,但这些容器不可选。或者他们可以选择吗? 也许MouseListener和访问系统默认的selected-background-colors可能是一种方法,但似乎有点费力
  3. 使用JTable或使用自定义单元格编辑器呈现“活动”JPanelJTree。但这些活动部分只会在“第二次”单击时做出反应,首先激活编辑器,然后执行 JPanel 的实际操作。这也是不可接受的。

为了获得更直观的印象,下面是一个示例来说明这可能意味着什么:

包含列表项的 JList,每个列表项都有两个功能 JButton

I need a Container with similar JPanels lined up one below the other which can be selected. I could:

  1. Use a JList with a custom renderer but those JPanels would be passive elements, that's not what I want.
  2. Implement my own Container with 'active' JPanels but those would not be selectable. Or could they made selectable?
    Maybe a MouseListener and access to the system default selected-background-colors could be a way but it seems a bit too much effort
  3. Use a JTable or JTree 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 the JPanel. 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 技术交流群。

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

发布评论

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

评论(3

鹿! 2024-12-09 15:35:08

正如您所发现的,简单地将 JPanel 放入 JList 中并不能完全按照您的意愿工作。 JPanel 将是被动的,不会接收事件 - 本质上所有发生的事情都是您的 JPanel 只是被绘制,它不是一个活动组件。

不要使用 JList 将面板放入列表中,而是使用类似列表的布局管理器,例如 BoxLayoutGridLayout。如果您希望所有面板具有相同的大小,请使用仅包含单个列的 GridLayout

As you've discovered, simply putting a JPanel inside a JList doesn't quite work as you'd like. The JPanel will be passive and won't receive events - essentially all that is happening is your JPanel 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 as BoxLayout or GridLayout. If you want all your panels to be the same size, use GridLayout with only a single column.

相思碎 2024-12-09 15:35:08

我不确定我是否理解你的“例子”。如果您需要两个功能按钮,请使用 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.

椒妓 2024-12-09 15:35:08

您的问题对我来说不清楚,也许这里

JTable,有一个 TableColumn 但没有 TableHeader,包含 JPanel 内有活动的 JComponents (您可以实现 TableCellEditor 对于所有 JComponent) 为 JComboBox, JButtonJTextField

your question(s) isn't clear for me, maybe here

there is JTable, with one TableColumn but without TableHeader, contains JPanel with active JComponents inside (you can implements TableCellEditor for all JComponents) as JComboBox, JButton and JTextField

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