Dimension (int minWidth, int maxWidth, int maxHeight) { Dimension paneSize = ((DocumentationEditorPane)getViewport().getView()).getPackedSize(minWidth, maxWidth); JScrollBar hBar = getHorizontalScrollBar(); boolean hasHBar = paneSize.width > maxWidth && hBar.isOpaque(); int hBarHeight = hasHBar ? hBar.getPreferredSize().height : 0; JScrollBar vBar = getVerticalScrollBar(); boolean hasVBar = paneSize.height + hBarHeight > maxHeight && vBar.isOpaque(); int vBarWidth = hasVBar ? vBar.getPreferredSize().width : 0; Insets insets = getInsets(); int preferredWidth = paneSize.width + vBarWidth + insets.left + insets.right; int preferredHeight = paneSize.height + hBarHeight + insets.top + insets.bottom; return new Dimension( Math.min(preferredWidth, maxWidth), Math.min(preferredHeight, maxHeight) ); }