public Dimension preferredLayoutSize(Container parent) 
    {
	/* Sync the (now obsolete) policy fields with the
	 * JScrollPane.
	 */
	JScrollPane scrollPane = (JScrollPane)parent;
	vsbPolicy = scrollPane.getVerticalScrollBarPolicy();
	hsbPolicy = scrollPane.getHorizontalScrollBarPolicy();
	Insets insets = parent.getInsets();
	int prefWidth = insets.left + insets.right;
	int prefHeight = insets.top + insets.bottom;
	/* Note that viewport.getViewSize() is equivalent to 
	 * viewport.getView().getPreferredSize() modulo a null
	 * view or a view whose size was explicitly set.
	 */
	Dimension extentSize = null;
	Dimension viewSize = null;
	Component view = null;
	if (viewport !=  null) {
	    extentSize = viewport.getPreferredSize();
            //Bug fix: always use the preferred size for the client.
	    //viewSize = viewport.getViewSize();
	    viewSize = viewport.getView().getPreferredSize();
	    view = viewport.getView();
	}
	/* If there's a viewport add its preferredSize.
	 */
	if (extentSize != null) {
	    prefWidth += extentSize.width;
	    prefHeight += extentSize.height;
	}
	/* If there's a JScrollPane.viewportBorder, add its insets.
	 */
	Border viewportBorder = scrollPane.getViewportBorder();
	if (viewportBorder != null) {
	    Insets vpbInsets = viewportBorder.getBorderInsets(parent);
	    prefWidth += vpbInsets.left + vpbInsets.right;
	    prefHeight += vpbInsets.top + vpbInsets.bottom;
	}
	/* If a header exists and it's visible, factor its
	 * preferred size in.
	 */
	if ((rowHead != null) && rowHead.isVisible()) {
	    prefWidth += rowHead.getPreferredSize().width;
	}
	if ((colHead != null) && colHead.isVisible()) {
	    prefHeight += colHead.getPreferredSize().height;
	}
	/* If a scrollbar is going to appear, factor its preferred size in.
	 * If the scrollbars policy is AS_NEEDED, this can be a little
	 * tricky:
	 * 
	 * - If the view is a Scrollable then scrollableTracksViewportWidth
	 * and scrollableTracksViewportHeight can be used to effectively 
	 * disable scrolling (if they're true) in their respective dimensions.
	 * 
	 * - Assuming that a scrollbar hasn't been disabled by the 
	 * previous constraint, we need to decide if the scrollbar is going 
	 * to appear to correctly compute the JScrollPanes preferred size.
	 * To do this we compare the preferredSize of the viewport (the 
	 * extentSize) to the preferredSize of the view.  Although we're
	 * not responsible for laying out the view we'll assume that the 
	 * JViewport will always give it its preferredSize.
	 */
	if ((vsb != null) && (vsbPolicy != VERTICAL_SCROLLBAR_NEVER)) {
	    if (vsbPolicy == VERTICAL_SCROLLBAR_ALWAYS) {
		prefWidth += vsb.getPreferredSize().width;
	    }
	    else if ((viewSize != null) && (extentSize != null)) {
		boolean canScroll = true;
		if (view instanceof Scrollable) {
		    canScroll = !((Scrollable)view).getScrollableTracksViewportHeight();
		}
		if (canScroll && (viewSize.height > extentSize.height)) {
		    prefWidth += vsb.getPreferredSize().width;
		}
	    }
	}
	if ((hsb != null) && (hsbPolicy != HORIZONTAL_SCROLLBAR_NEVER)) {
	    if (hsbPolicy == HORIZONTAL_SCROLLBAR_ALWAYS) {
		prefHeight += hsb.getPreferredSize().height;
	    }
	    else if ((viewSize != null) && (extentSize != null)) {
		boolean canScroll = true;
		if (view instanceof Scrollable) {
		    canScroll = !((Scrollable)view).getScrollableTracksViewportWidth();
		}
		if (canScroll && (viewSize.width > extentSize.width)) {
		    prefHeight += hsb.getPreferredSize().height;
		}
	    }
	}
        Dimension dim = new Dimension(prefWidth, prefHeight);
	return dim;
    }
public Dimension preferredLayoutSize(Container parent) 
    {

	JScrollPane scrollPane = (JScrollPane)parent;
	vsbPolicy = scrollPane.getVerticalScrollBarPolicy();
	hsbPolicy = scrollPane.getHorizontalScrollBarPolicy();
	Insets insets = parent.getInsets();
	int prefWidth = insets.left + insets.right;
	int prefHeight = insets.top + insets.bottom;

	Dimension extentSize = null;
	Dimension viewSize = null;
	Component view = null;
	if (viewport !=  null) {
	    extentSize = viewport.getPreferredSize();
           
	   
	    viewSize = viewport.getView().getPreferredSize();
	    view = viewport.getView();
	}

	if (extentSize != null) {
	    prefWidth += extentSize.width;
	    prefHeight += extentSize.height;
	}

	Border viewportBorder = scrollPane.getViewportBorder();
	if (viewportBorder != null) {
	    Insets vpbInsets = viewportBorder.getBorderInsets(parent);
	    prefWidth += vpbInsets.left + vpbInsets.right;
	    prefHeight += vpbInsets.top + vpbInsets.bottom;
	}

	if ((rowHead != null) && rowHead.isVisible()) {
	    prefWidth += rowHead.getPreferredSize().width;
	}
	if ((colHead != null) && colHead.isVisible()) {
	    prefHeight += colHead.getPreferredSize().height;
	}

	if ((vsb != null) && (vsbPolicy != VERTICAL_SCROLLBAR_NEVER)) {
	    if (vsbPolicy == VERTICAL_SCROLLBAR_ALWAYS) {
		prefWidth += vsb.getPreferredSize().width;
	    }
	    else if ((viewSize != null) && (extentSize != null)) {
		boolean canScroll = true;
		if (view instanceof Scrollable) {
		    canScroll = !((Scrollable)view).getScrollableTracksViewportHeight();
		}
		if (canScroll && (viewSize.height > extentSize.height)) {
		    prefWidth += vsb.getPreferredSize().width;
		}
	    }
	}
	if ((hsb != null) && (hsbPolicy != HORIZONTAL_SCROLLBAR_NEVER)) {
	    if (hsbPolicy == HORIZONTAL_SCROLLBAR_ALWAYS) {
		prefHeight += hsb.getPreferredSize().height;
	    }
	    else if ((viewSize != null) && (extentSize != null)) {
		boolean canScroll = true;
		if (view instanceof Scrollable) {
		    canScroll = !((Scrollable)view).getScrollableTracksViewportWidth();
		}
		if (canScroll && (viewSize.width > extentSize.width)) {
		    prefHeight += hsb.getPreferredSize().height;
		}
	    }
	}
        Dimension dim = new Dimension(prefWidth, prefHeight);
	return dim;
    }
public dimension preferredlayoutsize(container parent) { jscrollpane scrollpane = (jscrollpane)parent; vsbpolicy = scrollpane.getverticalscrollbarpolicy(); hsbpolicy = scrollpane.gethorizontalscrollbarpolicy(); insets insets = parent.getinsets(); int prefwidth = insets.left + insets.right; int prefheight = insets.top + insets.bottom; dimension extentsize = null; dimension viewsize = null; component view = null; if (viewport != null) { extentsize = viewport.getpreferredsize(); viewsize = viewport.getview().getpreferredsize(); view = viewport.getview(); } if (extentsize != null) { prefwidth += extentsize.width; prefheight += extentsize.height; } border viewportborder = scrollpane.getviewportborder(); if (viewportborder != null) { insets vpbinsets = viewportborder.getborderinsets(parent); prefwidth += vpbinsets.left + vpbinsets.right; prefheight += vpbinsets.top + vpbinsets.bottom; } if ((rowhead != null) && rowhead.isvisible()) { prefwidth += rowhead.getpreferredsize().width; } if ((colhead != null) && colhead.isvisible()) { prefheight += colhead.getpreferredsize().height; } if ((vsb != null) && (vsbpolicy != vertical_scrollbar_never)) { if (vsbpolicy == vertical_scrollbar_always) { prefwidth += vsb.getpreferredsize().width; } else if ((viewsize != null) && (extentsize != null)) { boolean canscroll = true; if (view instanceof scrollable) { canscroll = !((scrollable)view).getscrollabletracksviewportheight(); } if (canscroll && (viewsize.height > extentsize.height)) { prefwidth += vsb.getpreferredsize().width; } } } if ((hsb != null) && (hsbpolicy != horizontal_scrollbar_never)) { if (hsbpolicy == horizontal_scrollbar_always) { prefheight += hsb.getpreferredsize().height; } else if ((viewsize != null) && (extentsize != null)) { boolean canscroll = true; if (view instanceof scrollable) { canscroll = !((scrollable)view).getscrollabletracksviewportwidth(); } if (canscroll && (viewsize.width > extentsize.width)) { prefheight += hsb.getpreferredsize().height; } } } dimension dim = new dimension(prefwidth, prefheight); return dim; }