941,512 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 14902
  • Java RSS
Jul 28th, 2005
1

Getting horizontal scrollbars in a JTable

Expand Post »
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

Quote ...
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:
Java Syntax (Toggle Plain Text)
  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
Similar Threads
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,711 posts
since Nov 2004
Jul 29th, 2005
-1

Re: Getting horizontal scrollbars in a JTable

Hi everyone,

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

Richard West
Reputation Points: 25
Solved Threads: 10
Practically a Master Poster
freesoft_2000 is offline Offline
623 posts
since Jun 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Problem with Axis
Next Thread in Java Forum Timeline: OO assignment help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC