Difference between revisions of "Placing JComponent inside a JScrollPane"

From Catglobe Wiki
Jump to: navigation, search
Line 44: Line 44:
  
 
</source>  
 
</source>  
<br>Finally we have working Questionnaire Editor for MAC OS
+
<br>Finally we have working Questionnaire Editor for MAC OS. Happy Swing ;)
 
<br>
 
<br>
 
[[Image:MAC_Fixed.PNG]]
 
[[Image:MAC_Fixed.PNG]]
 +
=References=
 +
http://www.jasperpotts.com/blog/2007/06/full-size-jcomponent-in-jscrollpane/
 +
<br>http://explodingpixels.wordpress.com/2008/10/05/making-a-jtable-fill-the-view-without-extension/

Revision as of 10:05, 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
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:

    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 ;)
MAC Fixed.PNG

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/