Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Trim JScrollPane to fit JTable

843806Jul 31 2008 — edited Aug 1 2008
I have a JTable inside JScrollPane that has fixed number of rows. I have a function that calculates the preferred width of the table (taking into account the max width of every column for all the rows) so all the column�s text is visible.

JTable tagTable = new JTable(modulesValueTableModel);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(tagTable);
initColumnSizes(scrollPane, tagTable, modulesValueTableModel);

What I want is to set the viewport of the JScrollPane so it shows only table with all actual rows (no empty space at the bottom).
If I don�t re-adjust the height of the scrollPane I get the empty space at the bottom of the scrollPane.
When I set the viewport size to the preferred size of the table (as below) it truncates a little bit one column�s width (just few pixels) as if the preferred width of the tagTable was a little bit too small for the viewport:
scrollPane.getViewport().setPreferredSize(
new Dimension(tagTable.getPreferredSize().width,
tagTable.getPreferredSize().height));
When I set the viewport like below (using width of the scrollPane) it works as I want it to:
scrollPane.getViewport().setPreferredSize(
new Dimension(scrollPane.getViewport().getPreferredSize().width,
tagTable.getPreferredSize().height));
What am I missing here � it looks to me that the tagTable.getPreferredSize().width value is little bit smaller then the viewport needs to be so it hides a bit of one column.
Any help appreciated.
Janusz
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 29 2008
Added on Jul 31 2008
7 comments
985 views