Swing JTable 自定义渲染

发布于 2024-12-27 19:30:42 字数 603 浏览 0 评论 0原文

我有这种编程任务,没有使用 JavaFx,而是使用 Java Swing。我意识到我的知识仍然有限。

我只有一张 JTable。 但是,在这个 JTable 中,我需要一个自定义的单元格渲染器。 目标是制作这种 JTable: 示例图像

我当前的解决方案是: 示例图像

  1. 创建单个 JTable:
    • 获取每个列并使用自定义渲染器设置其 CellRenderer(如下)。
  2. 创建一个新类实现TableCellRenderer:
    • 在 getTableCellRendererComponent 中返回不同的 JPanel 使用 switch case 的方法(按列计数)。

经过几个小时又几个小时,我认为我当前的解决方案是相当艰巨的任务。因此,我的问题是:

创建此自定义 JTable 以实现上述主要目标的最简单方法是什么?

I have this kind of progamming task without JavaFx, instead it's Java Swing. I realized my knowledge is still limited.

I have one single JTable.
But, within this JTable I need a custome Cell Renderer.
The goal is to make this kind of JTable: Example image

My current solutions are: Example Image

  1. Create a Single JTable:
    • get each Column and set its CellRenderer with a custom Renderer (below).
  2. Create a new Class implements TableCellRenderer:
    • return different JPanel inside getTableCellRendererComponent
      method using switch case (as column counted).

After hours, and hours, I think my current solutions is quite daunting tasks. Thus, My question is:

What are the simplest method of creating this Custom JTable to achieve the main goal as mentioned above?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

舟遥客 2025-01-03 19:30:42

你有两个选择

1) JPanel 嵌套另一个 JComponents 并通过使用标准 LayoutManagers 解决这个问题,注意滚动不自然也不漂亮

2) JTable 与 JPanel 可以解决这个问题,注意滚动内部 JScrollPane 位于另一个 JScrollPane 中

you have two options

1) JPanel nested another JComponents and solve that by using standard LayoutManagers note scrolling isn't natural nor nice

2) JTable with JPanel can solve that, notice about scrolling inner JScrollPane inside another JScrollPane

第几種人 2025-01-03 19:30:42

我已经面临这个问题有一段时间了,我决定自己解决。扩展表的现有实现,添加一些我对表的期望的概念,并为此编写一些编辑器/侦听器。都一样,但是有一个树表。

我正在开发一个名为 SUMI 的项目。

它包含一个 java 包 (ar.com.tellapic.sumi.treetable),它是 JXTreeTable 来自 SwingLabs

该项目正在开发中,我还没有提供任何文档。您可以通过创建渲染器以及编辑器(如果需要)来完成您想要的操作,以便最后将操作附加到每个对象。

如果您决定使用它并且需要帮助,请给我发送电子邮件,我会毫无问题地帮助您。

或者,您可以自己阅读源代码。

问候,

编辑(再次):

为了澄清一点这个答案,我刚刚在 项目 wiki 并将相关代码放在那里。如果有人认为应该在此处插入代码,请告诉我。

基本上,我尝试解释如何通过使用我的项目的一部分来找到使用 JTable 和您的具体需求时可能发现的渲染器/编辑器问题的直接解决方案,以获得如下内容:

< a href="https://i.sstatic.net/l0sxN.png" rel="nofollow noreferrer">

注意屏幕截图是点击相应的勾选按钮后拍摄。

I've been facing this problem for a while, and I decided to do it myself. Extending the existing implementation of a table, adding some concepts for what I expect from a table, and writting some editors/listeners for that. All the same, but with a treetable.

I'm working on this project called SUMI.

It contains a java package (ar.com.tellapic.sumi.treetable) that is an extension of a JXTreeTable from SwingLabs.

The project is being developed and I didn't provide any documentation yet. You can do what you want by creating a renderer and if needed, an editor, for lastly attaching actions to each object.

If you decide to use it and you need help, email me, I'll help you without any problem.

Or, you could read the source by your own.

Regards,

EDITED (again):

To clear a little bit this answer, I've just created a wiki page in the project wiki and put the relevant code there. If someone feels that the code should be inserted here, please let me know.

Basically, I try to explain how to find a straight solution to the renderer/editor problems you may find using JTable with your specifics needs by using part of my project, in order to get something like this:

Note that the screenshot was taken after clicking on the respective tick-button.

儭儭莪哋寶赑 2025-01-03 19:30:42

按照 @mKorbel 的建议为一行创建嵌套面板后,您可以将任意数量的面板添加到 JScrollPane 中的 GridLayout(0, 1) 中。如果渲染许多行成为问题,您可以采用 JTable 使用的相同方法,如此处。

Once you create a nested panel for one row, as suggested by @mKorbel, you can add any number of them to a GridLayout(0, 1) in a JScrollPane. If rendering many rows becomes an issue, you can adopt the same approach used by JTable, illustrated here.

深空失忆 2025-01-03 19:30:42

尽管可以通过单元格渲染器和单元格编辑器将 JTable 定制为您想要的任何内容,但它永远不是首选,因为您必须为此进行大量混乱的编码。相反,对于您的问题,我建议使用 JScrollPane 并将您的组件(视图面板作为示例 jTable )添加到其 viewPort 。

对于此实现,用扩展 JPanel 的自定义类表示每一行。并在其中添加所需的行组件(也可以是 jlabel、jtextfields 甚至 jpanel 等任何组件)。为了简单起见,您可以对行面板使用空布局,并在您想要的任何位置添加组件。

我希望这能帮助您解决问题。如果您在此实施过程中遇到任何问题,请随时再次询问。

Even though, JTable can be customized to whatever you desire through cell renderer and cell editors, it is never preferred because you have to do a lot of messy codings for that. Instead, for your problem, I suggest to use JScrollPane and add your component (view panel as your sample jTable ) to its viewPort.

For this implementation, represent each rows with your custom class that extends JPanel. And add the required row components (that may be any components like jlabel, jtextfields or even jpanel too) in it. For the simplicity, you can use null layout for the row panel and add the components at any location you want.

I hope this will help you workout with your problem. If you got any problem in this implementation, feel free you ask again.

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