JTable 标题和间隔行

发布于 2024-11-29 15:32:37 字数 864 浏览 0 评论 0原文

我是 Swing 新手,今天一直在努力使用 JTable 组件。因为我正在尝试制作一个有点花哨的表格,所以我还无法在任何地方找到我正在寻找的特定答案。

我希望在表格的最顶部有一组标题。这意味着 3 或 4 行跨越整个表,并对各个列进行“分组”,本质上是对它们进行分区。

示例(请原谅眼睛疲劳):

////////////////////////////////ANIMALS//////////////////////////
/////////Mammals///////////////////////Rodents///////////////////
////People/////Dogs//////////Rats/////Rodents of Unusual Size////

其中斜线(“/”)表示空白。第一个标题为“ANIMALS”,指的是表中的所有列。第二个标头向下划分 ANIMALS,依此类推。在第三个也是最后一个标题之后,我们到达了实际的表格,其中每行将有 4 列,每列代表一种动物。

显然这是一个例子,而且可能是一个糟糕的例子,但这就是我所说的“分区标头”的意思。

我的问题: 我不明白 JTableHeader 和 TableColumnModel 如何支持此功能。如果他们这样做了,有人可以将我推向正确的方向吗?如果他们不这样做,这肯定不是 Swing 应用程序第一次需要这样的东西。有人知道任何第三方组件吗?

另外,我希望能够在整个表中散布“间隔”或跨行,代表一组行。这些间隔符将存在于表格内部(不像标题一样位于表格顶部),但会跨越每一列并具有单个标签,表示后续行所代表的组的名称。同样,我没有看到 Swing 支持这一点,有什么想法吗?

谢谢,我为这个真正糟糕的例子/图画道歉。

I'm new to Swing and have been struggling with the JTable component today. Because I'm trying to make a somewhat-fancy table I haven't been able to find the particular answers I'm looking for anywhere yet.

I would like to have a "group" of headers at the very top of the table. This means 3 or 4 rows that span the whole table, and "group" the various columns, essentially partitioning them.

Example (pardon the eye strain):

////////////////////////////////ANIMALS//////////////////////////
/////////Mammals///////////////////////Rodents///////////////////
////People/////Dogs//////////Rats/////Rodents of Unusual Size////

Where slashes ("/") denote whitespace. The first header, titled "ANIMALS" refers to all the columns in the table. The second header down partitions ANIMALS, so on and so forth. After the third and final header we arrive the actual table, where each row will have 4 columns, one for each type of Animal.

Obviously this is an example, and probably a bad one, but this is what I mean by "partitioning headers".

My question:
I don't see how JTableHeader and TableColumnModel support this functionality. If they do, can someone nudge me in the right direction. If they don't, this surely can't be the first time a Swing app has needed something like this. Any 3rd party components anyone knows of?

Also, I'd like to be able to have "Spacer" or Spanning Rows sprinkled throughout my table, representing a group of rows. These Spacers would exist inside the table (not on top of it like a header), but would span every column and have a single label, representing the name of the group represented by the subsequent rows. Again, I don't see Swing supporting this, any ideas?

Thanks and I do apologize for the truly-awful example/drawing.

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

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

发布评论

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

评论(4

天冷不及心凉 2024-12-06 15:32:37

您也许能够改编这个古老的多行标题示例。

You might be able to adapt this venerable Multiple Row Header example.

你曾走过我的故事 2024-12-06 15:32:37

默认情况下,JTable 中只有一行标题(它不是 JTable 的一行,而是 JTableHeader)。无论如何,您可以实现一个 TableCellRenderer以类似于标题的方式显示前 n 个非标题行。

步骤大致如下:

  1. 实现 TableCellRenderer 以显示所需行的普通 JTable 单元格(如标题单元格)

,例如:

public Component getTableCellRendererComponent(JTable table, Object value,
    boolean isSelected, boolean hasFocus, int row, int column){   

    this.setOpaque(true);
    TableCellRenderer renderer = table.getTableHeader().getDefaultRenderer();
    return renderer.getTableCellRendererComponent(table, value, isSelected,
          hasFocus, row, column);
}
  1. 扩展 JTable 并实现以下方法以返回所需的单元格渲染器:

    public TableCellRenderer getCellRenderer(int row, int column){}
    

无论如何,根据您的要求,还有另一个问题需要解决:JTable 有固定数量的列,因此您必须找到一个达到该结果的解决方法。

By default there is only one row of header in a JTable (and it's not properly a row of the JTable it's a JTableHeader). Anyway you can implement a TableCellRenderer that display the first n non-header-rows in a fashion like it seems to be header.

The steps are more ore less the following :

  1. Implement TableCellRenderer to display ordinar JTable's cell of desired row like header's cell

like:

public Component getTableCellRendererComponent(JTable table, Object value,
    boolean isSelected, boolean hasFocus, int row, int column){   

    this.setOpaque(true);
    TableCellRenderer renderer = table.getTableHeader().getDefaultRenderer();
    return renderer.getTableCellRendererComponent(table, value, isSelected,
          hasFocus, row, column);
}
  1. Extend JTable and implement the following method in order to return the desidered cell renderer:

    public TableCellRenderer getCellRenderer(int row, int column){}
    

Anyway with your requirements there is another problem to solve: a JTable has a fixed number of column, so you have to find a workaround to achive that result.

清眉祭 2024-12-06 15:32:37

核心 Swing 不支持单元格(行/列)分组或拆分,无论是标题还是表本身。 JTable 的设计不是通用网格。试图在其中调整一些类似网格的行为和视觉效果将与它的设计相冲突。可以做到,但实现起来并不容易。 @Trashgod 指出了其中一些战斗(它们仍然有效吗?它们看起来像老式的第一代 Tame 示例?)。 JIDE 是支持它的商业产品(免责声明:从未在生产中使用过它:- )

cell (both row/column) grouping or splitting is not supported by core Swing, neither for the header nor the table itself. A JTable by design is not a general-purpose Grid. Trying to tweak some grid-like behaviour and visuals into it would be fighting its design. Can be done, but it's not trivial to achieve. @Trashgod pointed to some of those fights (do they still work? they look like the oldish first-generation Tame examples?). A JIDE is a commercial product which supports it (disclaimer: never used it in production :-)

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