tags:

views:

1371

answers:

4

I am trying to make a JTable that has column spans available. Specifically, I am looking to nest a JTable inside another JTable, and when the user clicks to view the nested table, it should expand to push down the rows below and fill the empty space. This is similar to what you see in MS Access where you can nest tables, and clicking the expand button on a row will show you the corresponding entries in the nested table.

If someone knows of a way to perform a column span with JTable, can you please point me in the right direction? Or if you know of an alternative way to do this, I am open to suggestions. The application is being built with Swing. Elements in the table, both high level and low level, have to be editable in any solution. Using nested JTables this won't be a problem, and any other solution would have to take this into consideration as well.

A: 

You need to write your own TableUI for the master table. It can also helpful to use your own TableModel to save additional data like if a row is expanded. But this is optional.

I write an equals TableUI that expand a row and show an text editor. In the TableUI you need to change the the row hight dynamically with table.setRowHeight(height). Also it is necessary to copy some stuff from the BaseTableUI because you can not access the private stuff.

Horcrux7
I still don't see how you would go about telling your JTable to merge columns in a given row. I know how to insert a new row into the table, but how do I tell the table to merge those cells?
Elie
+2  A: 

As a pointer in the right direction, try this article at SwingWiki that explains the TableUI method of column spanning quite well. Before this, I also tried some alternatives such as overriding the TableCellRenderer paint methods without much success.

cgull
A: 

I've seen this feature abused so much in Access, I wonder how often it is really useful to end users. What about having two tables: The first is the "parent" table that displays your primary data, and the second which displays the "nested" table for the selected row on the main table?

Draemon
A: 

I've seen this feature abused so much in Access, I wonder how often it is really useful to end users. What about having two tables: The first is the "parent" table that displays your primary data, and the second which displays the "nested" table for the selected row on the main table?