tags:

views:

42

answers:

2

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.

+1  A: 

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.

akf
This throws Stackoverflow error when i run in 1.4 :(
Harish
Interesting - I managed to get it to work on jdk6, and may use it in future projects
tucuxi
+2  A: 

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

tucuxi