This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JTable / Columns - rows inversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable / Columns - rows inversion" Watch "JTable / Columns - rows inversion" New topic

JTable / Columns - rows inversion

Charles Van Damme

Joined: Feb 24, 2009
Posts: 19
Good day.

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 ?

Thanks for any idea, code example, ...
Rob Spoor

Joined: Oct 27, 2005
Posts: 19908

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.

How To Ask Questions How To Answer Questions
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
just a thought...

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.
Martin Vajsar

Joined: Aug 22, 2010
Posts: 3611

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.
Don't get me started about those stupid light bulbs.
subject: JTable / Columns - rows inversion