Difference between revisions of "Placing JComponent inside a JScrollPane"
(Created page with 'Add Your Content Here category:Technical guidelines =Introduction= You have some component in a scroll pane and when its preferred height is less than the scollpane area …') |
|||
Line 5: | Line 5: | ||
=Introduction= | =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 | 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 | ||
− | + | <br> | |
+ | <source lang="java"> | ||
this.setFillsViewportHeight(true); | this.setFillsViewportHeight(true); | ||
− | + | </source> | |
But back to JDK 5, you must implement it by your self. | But back to JDK 5, you must implement it by your self. | ||
=Our problem= | =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 | 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 | ||
<br> | <br> | ||
− | [ | + | [[Image:MAC_bug.png]] |
+ | =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: | ||
+ | <source lang="java"> | ||
+ | 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; | ||
+ | } | ||
+ | |||
+ | |||
+ | </source> | ||
+ | <br>Finally we have working Questionnaire Editor for MAC OS | ||
+ | <br> | ||
+ | [[Image:MAC_Fixed.PNG]] |
Revision as of 10:01, 11 November 2009
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;
}