我在 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.
发布评论
评论(2)
您可以在模型或视图级别合并 JTable 列。 Swing 中两者都没有得到特别好的支持(也就是说,没有“jTable.mergeColumns(colA, colB)”内置功能)。
假设您有一个像这样的表:
如果您在模型级别合并列 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()
方法如下:在 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:
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
'ssetDataVector()
method by the following:Works fine for me under jdk6
我在 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 inJTable
and overriding the table UI to do the painting.