Difference between revisions of "Placing JComponent inside a JScrollPane"
Wikicatglobe (talk | contribs) |
|||
(One intermediate revision by one other user not shown) | |||
Line 1: | Line 1: | ||
+ | <accesscontrol>Main:MyGroup</accesscontrol> | ||
Add Your Content Here | Add Your Content Here | ||
Latest revision as of 05:42, 18 October 2013
<accesscontrol>Main:MyGroup</accesscontrol> Add Your Content Here
Introduction
You have some component in a scroll pane and when its preferred height is less than the scollpane area then you want it to stretch to fill the scollpane. But when it grows bigger than the scollpane area you would like it show the scrollbars. This is very useful when you want to be able to click in the spare space to clear selection or in the case of drag and drop where you want to be able to drop into the empty area. From JDK 6, in order to do this, simply use function
this.setFillsViewportHeight(true);
But back to JDK 5, you must implement it by your self.
Our problem
Recently, we have some client using MAC PC. MAC OS only supports JDK 5. That's the reason why the supported function i mentioned above can't be use. It leads to our GUI for question properties dialog broken
Solution
Notice that our question properties dialog contains many sub-class of PropertyTable ( also a subclass of JTable. All we have to do is implement Scrollable and write some addition function to the class:
public Dimension getPreferredScrollableViewportSize() {
return getPreferredSize();
}
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
return (orientation == SwingConstants.HORIZONTAL)? visibleRect.width : visibleRect.height;
}
public boolean getScrollableTracksViewportHeight() {
boolean tracks = false;
if (getParent() instanceof JViewport) {
tracks = getPreferredSize().getHeight() <
((JViewport) getParent()).getExtentSize().getHeight();
}
return tracks;
}
public boolean getScrollableTracksViewportWidth() {
return true;
}
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
return getScrollableBlockIncrement(visibleRect, orientation, direction)/5;
}
Finally we have working Questionnaire Editor for MAC OS. Happy Swing ;)
References
http://www.jasperpotts.com/blog/2007/06/full-size-jcomponent-in-jscrollpane/
http://explodingpixels.wordpress.com/2008/10/05/making-a-jtable-fill-the-view-without-extension/