I fixed 3 methods in JEditorPane which make this component behave more nicely in scolling containers. Took me quite a while to determine the correct behaviour (using BeanShell) and then found that this is even specified.. duh
2006-02-22 Roman Kennke <[EMAIL PROTECTED]> * javax/swing/JEditorPane.java (getPreferredSize): Rewritten to behave like the reference impl. (getScrollableTracksViewportWidth): Likewise. (getScrollableTracksViewportHeight): Likewise. /Roman
Index: javax/swing/JEditorPane.java =================================================================== RCS file: /cvsroot/classpath/classpath/javax/swing/JEditorPane.java,v retrieving revision 1.30 diff -u -r1.30 JEditorPane.java --- javax/swing/JEditorPane.java 9 Dec 2005 19:28:38 -0000 1.30 +++ javax/swing/JEditorPane.java 22 Feb 2006 09:54:41 -0000 @@ -38,6 +38,7 @@ package javax.swing; +import java.awt.Container; import java.awt.Dimension; import java.io.IOException; import java.io.InputStream; @@ -682,27 +683,59 @@ } /** - * Returns the preferred size for the JEditorPane. + * Returns the preferred size for the JEditorPane. This is implemented to + * return the super's preferred size, unless one of + * [EMAIL PROTECTED] #getScrollableTracksViewportHeight()} or + * [EMAIL PROTECTED] #getScrollableTracksViewportWidth()} returns <code>true</code>, + * in which case the preferred width and/or height is replaced by the UI's + * minimum size. + * + * @return the preferred size for the JEditorPane */ public Dimension getPreferredSize() { - return super.getPreferredSize(); + Dimension pref = super.getPreferredSize(); + if (getScrollableTracksViewportWidth()) + pref.width = getUI().getMinimumSize(this).width; + if (getScrollableTracksViewportHeight()) + pref.height = getUI().getMinimumSize(this).height; + return pref; } + /** + * Returns <code>true</code> when a Viewport should force the height of + * this component to match the viewport height. This is implemented to return + * <code>true</code> when the parent is an instance of JViewport and + * the viewport height > the UI's minimum height. + * + * @return <code>true</code> when a Viewport should force the height of + * this component to match the viewport height + */ public boolean getScrollableTracksViewportHeight() { - /* Container parent = getParent(); - return (parent instanceof JViewport && - parent.isValid());*/ - return isValid(); + // Tests show that this returns true when the parent is a JViewport + // and has a height > minimum UI height. + Container parent = getParent(); + return parent instanceof JViewport + && parent.getHeight() > getUI().getMinimumSize(this).height; } + /** + * Returns <code>true</code> when a Viewport should force the width of + * this component to match the viewport width. This is implemented to return + * <code>true</code> when the parent is an instance of JViewport and + * the viewport width > the UI's minimum width. + * + * @return <code>true</code> when a Viewport should force the width of + * this component to match the viewport width + */ public boolean getScrollableTracksViewportWidth() { - /*Container parent = getParent(); - return (parent instanceof JViewport && - parent.isValid());*/ - return isValid(); + // Tests show that this returns true when the parent is a JViewport + // and has a width > minimum UI width. + Container parent = getParent(); + return parent != null && parent instanceof JViewport + && parent.getWidth() > getUI().getMinimumSize(this).width; } public URL getPage()