| | |
Getting horizontal scrollbars in a JTable
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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
Could be worth it if you have several JTables in your project or you rely heavily on autoresize.
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:
Java Syntax (Toggle Plain Text)
/** * Returns false to indicate that horizontal scrollbars are required * to display the table while honoring perferred column widths. Returns * true if the table can be displayed in viewport without horizontal * scrollbars. * * @return true if an auto-resizing mode is enabled * and the viewport width is larger than the table's * preferred size, otherwise return false. * @see Scrollable#getScrollableTracksViewportWidth */ public boolean getScrollableTracksViewportWidth() { if (autoResizeMode != AUTO_RESIZE_OFF) { if (getParent() instanceof JViewport) { return (((JViewport)getParent()).getWidth() > getPreferredSize().width); } } return false; }
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.
•
•
Join Date: Jun 2004
Posts: 609
Reputation:
Solved Threads: 8
Hi everyone,
Hmm 1998. Now its 2005. Seven years. Makes you wonder if that bug will be fixed.
Richard West
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
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
![]() |
Similar Threads
- Laptop LCD screen horizontal lines (Monitors, Displays and Video Cards)
- New font size (DaniWeb Community Feedback)
- Code line numbers - causing paste problems (DaniWeb Community Feedback)
- Horizontal lines across display (Monitors, Displays and Video Cards)
- JTable Limitation or Not? (Java)
Other Threads in the Java Forum
- Previous Thread: Problem with Axis
- Next Thread: OO assignment help
Views: 8958 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for Java
3d @param affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth byte c# chat class classes click client code color compare component corrupted database detection draw eclipse error event exception file fractal game givemetehcodez graphics gui guitesting helpwithhomework html ide image input integer j2me java java.xls javaprojects jmf jni jpanel julia keytool keyword linux list loop map method methods mobile netbeans newbie number object oracle pong print problem producer program programming project read recursion reflection replaysolutions rim scanner screen server set size sms socket sort sql string swing terminal test threads time transfer tree web windows






