Skip to main content

Adjusting size of two tables in a single JScrollPane

2 replies [Last post]
navinkjha
Offline
Joined: 2004-12-28

I have two tables in a jscrollpane attached like this:
treeTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
flatTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);

setCorner(JScrollPane.UPPER_LEFT_CORNER, flatTable.getTableHeader());
setRowHeaderView(flatTable);
setViewportView(treeTable);

If I need to reverse scrolling behavior I do this:

treeTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
flatTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

setCorner(JScrollPane.UPPER_LEFT_CORNER, treeTable.getTableHeader());
setRowHeaderView(treeTable);
setViewportView(flatTable);

The problem is when I reverse the scrolling behavior the sizes of tables don't adjust accordingly so columns don't have desired width as before.

Any suggestion on this one ?

-Navin

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kschaefe
Offline
Joined: 2006-06-08

Try using pack() on each table.

Karl

navinkjha
Offline
Joined: 2004-12-28

Karl,

This worked:

if (treeTableScrollable) {
// auto resize options
treeTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
flatTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);

setCorner(JScrollPane.UPPER_LEFT_CORNER, flatTable.getTableHeader());
setRowHeaderView(flatTable);
setViewportView(treeTable);

treeTable.packAll();
if (flatTable != null)
flatTable.packAll();
flatTable.setPreferredScrollableViewportSize(flatTable.getPreferredSize());
} else {
// auto resize options
treeTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
flatTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

setCorner(JScrollPane.UPPER_LEFT_CORNER, treeTable.getTableHeader());
setRowHeaderView(treeTable);
setViewportView(flatTable);
treeTable.packAll();
if (flatTable != null)
flatTable.packAll();
treeTable.setPreferredScrollableViewportSize(treeTable.getPreferredSize());
}

Essentially setPreferredScrollableViewportSize did the job. I thought packAll() should do the job. Not sure why that alone doesn't do the job. I also tried doing invalidate on the scrollpane. I think that is needed anyway for row selection to work properly. Before that I found at some point different rows of two tables being selected at the same time once I moved columns between the two tables. If I can still reproduce that problem I will post it in a separate thread.