• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sizing a JScrollPane width to its contained JTable

 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
- posts about getPreferredScrollableViewportSize by Jeanette on SO

soo crazy this formatting rulles, here :-)
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Korbel, thank you for the link.
reply
    Bookmark Topic Watch Topic
  • New Topic