For an optimal occupation of a GUI panel, it should be better that my narrow table (2 columns / lot of rows) be horizontal instead of vertical. That means:
- 2 rows / lots of columns
- headers not on top of columns but at the left side
- Selection by column instead of by row (light blue)
... as if the table had revolved on its diagonal
(The table then is to be enclosed in a JScrollPane)
I'm using the JTable class of 'javax.swing' package of the Java SE 6 platform. I don't see properties, methods nor models to operate this rotation. Isn't it achievable in Java SE and Swing ?
I thought this Line/Row inversion was a frequently asked requirement.
Solution ?: A subclass of JTable where columns and row properties are managed to offer the inversion. But then,
- what with the elimination of the original column headers ?
- how to have row headers ?
The ideal solution is to write a new look&feel class; essentially a sub class of TableUI. It could use BasicTableUI and MetalTableUI as templates to write it. However, that's not going to be easy.
Apparently there is no MetalTableUI.
subclass JTable to have no header, 1 column and only 2 rows.
give it an index field
probably needs a preferred width
add a listener so that if one row is selected, both rows are highlighted
set up a JPanel as a GridLayout(1,0)
add to the panel the 1st table, but only this one add the header info to the 2 rows,
make the cells uneditable, change the colors to header colors (from UIManager),
possibly remove listener/s (depending on what you do/don't want to happen), give it
an index of -1(?)
now add to the panel all the other tables, index 0 to whatever (where the index is used
to ID the table, similar to a column index)
something like the above might work, probably won't, but worth a shot.
Rob Spoor wrote:The ideal solution is to write a new look&feel class; essentially a sub class of TableUI. It could use BasicTableUI as templates to write it. However, that's not going to be easy.
This does look like an elegant solution. Unfortunately, the "horizontal" orientation of JTable is reflected in the table model classes too. For example, columns can have their widths specified, while rows have height. In the transposed representation, columns would have a height, while rows would have a width. Changing the look and feel might therefore not be enough, you might need to enhance the underlying data model too.
I had created an extension of the JTable, which has row headers too. I planned to implement generic row/column inversion at the level of the table model to this extended table, but then balked at it. I had only one use case for that and it simply would not pay off.