合并 JTable 中的列

发布于 2024-08-28 04:46:06 字数 750 浏览 7 评论 0 原文

我在 JTable 工作,我有这样的需求。 假设有 4 列,即 10,20,30,40

现在值通常为 10-20 20-30 和 30-40 因此我们很容易显示该范围的名称。

但最近这些值开始随机出现,如 15-25 10-25,25-30

在这种情况下,我们的 JTable 应该动态调整行的大小,使其代表该范围,这意味着它不应干扰现有单元格,而仅偏离正常范围的行。

更准确地说,我应该能够根据单元格的内容合并和拆分单元格。

编辑:就像这样。一个人被分配了一个

10|          |20|          |30|

  |----------|
                |----------|

代表 10-20 和 20-30 的特定时间点的任务。第一行 10,20,30 是列名称。第二行是一个框的图形表示,代表10-20 和 20-30。现在,如果出现值 15-25

10|          |20|          |30|

  |----------|  |----------|
       |-----------|
           |-----------|
    |-------------|

  |----------|  |----------|

实际上,第一行和第四行之间没有间隙,只是为了表明它们是单独的单元格。现在,因为数据处于 15-25 等中间范围内我们必须像上面我发布的那样重新对齐单元格形状。

I am working in JTable and I have a requirement like this.
Say There are 4 columns namely 10,20,30,40

Now the value usually comes like 10-20 20-30 and 30-40
So it was easy for us to display the name for this range.

But recently the values have started to come randomly like 15-25 10-25,25-30

In this case our JTable should dynamically adjust the size of the row such that it represents that range only meaning it should not disturb the existing cells and only rows which diverge from the normal range.

TO be more precise I should be able to merge and split cells based on the content of the cell.

EDIT:Its like this.A person is assigned a task for a particular point of time

10|          |20|          |30|

  |----------|
                |----------|

represents 10-20 and 20-30 .The first line 10,20,30 are the column names.The second line is graphical representation of a box representing 10-20 and 20-30.Now if a value 15-25 comes

10|          |20|          |30|

  |----------|  |----------|
       |-----------|
           |-----------|
    |-------------|

  |----------|  |----------|

Actually there is no gap between in the first and fourth row just to show that they are seperate cells.Now since data comes in the intermediate ranges of like 15-25 where we have to re align the cell shape as above I posted this.

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

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

发布评论

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

评论(2

落叶缤纷 2024-09-04 04:46:06

您可以在模型或视图级别合并 JTable 列。 Swing 中两者都没有得到特别好的支持(也就是说,没有“jTable.mergeColumns(colA, colB)”内置功能)。

假设您有一个像这样的表:

A  B  C
1  2  3
2  4  6    

如果您在模型级别合并列 B 和 C,您的模型会说只有 2 列(A 和 B+C),并且会定义第二个 'B 和C'列为2+3=5和4+6=10;或者它可以表示该列字符串的值,例如“2 3”和“4 6”。

如果在视图级别合并这些列,则必须为 B 列和 C 列提供自己的 ColumnCellRenderer,并以某种方式避免绘制网格线。我不推荐这种方法。

最后的选择是同时更改模型和视图:您可以修复 akf 指向的代码(可以从 http://www.codeguru.com/java/articles/139.shtml) 通过替换 AttributiveCellTableModel< 的开头/code> 的 setDataVector() 方法如下:

  public void setDataVector(Vector newData, Vector columnNames) {
    if (newData == null)
      throw new IllegalArgumentException("setDataVector() - Null parameter");
    dataVector = newData;
    columnIdentifiers = new Vector(columnNames);

在 jdk6 下对我来说工作正常

You can merge JTable columns at either the Model or View levels. Neither is especially well supported in Swing (that is, there is no "jTable.mergeColumns(colA, colB)" built-in functionality).

Say that you have a table like this:

A  B  C
1  2  3
2  4  6    

If you merge columns B and C at the Model level, your Model would say that there are only 2 columns (A and B+C), and would define the values of the second 'B and C' column to be, say 2+3=5 and 4+6=10; or it can say that the values of this column strings, such as "2 3" and "4 6".

If you merge these columns at the View level, you have to supply your own ColumnCellRenderer for columns B and C, and somehow avoid painting grid lines. I do not recommend this approach.

The final option is to change model and view at the same time: you can fix the code pointed at by akf (a zipped version can be downloaded from http://www.codeguru.com/java/articles/139.shtml) by substituting the start of AttributiveCellTableModel's setDataVector() method by the following:

  public void setDataVector(Vector newData, Vector columnNames) {
    if (newData == null)
      throw new IllegalArgumentException("setDataVector() - Null parameter");
    dataVector = newData;
    columnIdentifiers = new Vector(columnNames);

Works fine for me under jdk6

吾家有女初长成 2024-09-04 04:46:06

我在 Swing 中看到此实现的唯一地方是 第三个示例在此链接中。这里介绍的关键方面是重写 JTable 中的 getCellRect 方法并重写表 UI 来进行绘制。

The only place that I have seen this implemented in Swing is in the third example in this link. The key aspects that are covered here are overriding the getCellRect method in JTable and overriding the table UI to do the painting.

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