• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sizing a JScrollPane width to its contained JTable

 
Darryl Burke
Bartender
Posts: 5155
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Returning true from getScrollableTracksViewportWidth() makes the table (or other Scrollable) size its width to match the viewport. I need the opposite: for the viewport to expand its width to accommodate the table (of course, limited by the constraints imposed by the scroll pane's container's layout manager) without reducing column widths i.e. setAutoResizeMode(AUTO_RESIZE_OFF).

I have achieved this by overriding getPreferredScrollableViewportSize():Now when the scroll pane is added at BorderLayout.CENTER and the frame is pack()ed, all columns of the table are displayed, without need for a horizontal scrollbar.

I would like to know if anyone knows of a better, less hackish way to do this.
 
m Korbel
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to know if anyone knows of a better, less hackish way to do this.

- doesn't exist for JTable with reasonable numbers of rows,

- but there is one issue that can messing up this good idea, horizontal JScrollBar
to cuts JTables Dimension (setAutoResizeMode(AUTO_RESIZE_OFF), by calculating
with (without JScrollBar) you would need to put there empty space for JScrollBar
(by leaving ...if this is nice or not)

- because this idea is just for reasonable numbers of rows (JFrame.pack()), I'd be ends
with, by using JTable.setPreferredScrollableViewportSize( new Dimension( int, int ) );,
where is super.getPreferredSize().width and height is limited (to reasonable) number
of rows
 
Darryl Burke
Bartender
Posts: 5155
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, m Korbel. My table is not likely to ever contain more than about 300 rows, and the column widths are computed by Rob Camick's TableColumnAdjuster. Columns can also be added or removed, using Rob's TableColumnManager.

Although not yet implemented, I plan to allow the user to pack() the frame at any time, so I wouldn't want to setPreferredScrollableViewportSize to any precomputed value.
 
m Korbel
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
- posts about getPreferredScrollableViewportSize by Jeanette on SO

soo crazy this formatting rulles, here :-)
 
Darryl Burke
Bartender
Posts: 5155
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Korbel, thank you for the link.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!