Getting horizontal scrollbars in a JTable

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Getting horizontal scrollbars in a JTable

 
0
  #1
Jul 28th, 2005
Problem

Due to a rather annoying and very old bug in JTable you will never get a horizontal scrollbar on a JTable even if the table is wider than the JScrollPane you placed it in. According to the bug report this is due to an error in the handling of the autoresize functionality of JTable.

Solution

There are two ways around this: the first and easiest (which may be good enough for most people) is to turn OFF autoresize on your JTable using table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); (where 'table' is a JTable).

The second (more involved) way is to fix the responsible function in JTable (which would involve creating a custom subclass of JTable).

The code that you need is listed (I haven't tried it!) in bug report #1027936

Override JTable.getScrollableTracksViewportWidth() to honor the table's
preferred size and show horizontal scrollbars if that size cannot be
honored by the viewport if an auto-resize mode is selected. Here is
the suggested change:
  1. /**
  2.   * Returns false to indicate that horizontal scrollbars are required
  3.   * to display the table while honoring perferred column widths. Returns
  4.   * true if the table can be displayed in viewport without horizontal
  5.   * scrollbars.
  6.   *
  7.   * @return true if an auto-resizing mode is enabled
  8.   * and the viewport width is larger than the table's
  9.   * preferred size, otherwise return false.
  10.   * @see Scrollable#getScrollableTracksViewportWidth
  11.   */
  12. public boolean getScrollableTracksViewportWidth() {
  13. if (autoResizeMode != AUTO_RESIZE_OFF) {
  14. if (getParent() instanceof JViewport) {
  15. return (((JViewport)getParent()).getWidth() > getPreferredSize().width);
  16. }
  17. }
  18. return false;
  19. }

Could be worth it if you have several JTables in your project or you rely heavily on autoresize.
Last edited by happygeek; Oct 29th, 2006 at 7:07 am. Reason: Formatting
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 609
Reputation: freesoft_2000 is an unknown quantity at this point 
Solved Threads: 8
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Re: Getting horizontal scrollbars in a JTable

 
0
  #2
Jul 29th, 2005
Hi everyone,

Hmm 1998. Now its 2005. Seven years. Makes you wonder if that bug will be fixed.

Richard West
Microsoft uses "One World, One Web, One Program" as a slogan.
Doesn’t that sound like "Ein Volk, Ein Reich, Ein Führer" to you, too?
— Eric S. Raymond

Tell me what type of software do you like and what would you pay for it

http://www.daniweb.com/techtalkforums/thread19660.html
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum


Views: 8958 | Replies: 1
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC