Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4839118

JEditorPane.getPreferredSize() behaves inconsistently

XMLWordPrintable

    • Fix Understood
    • x86
    • windows_xp



      Name: jk109818 Date: 03/27/2003


      FULL PRODUCT VERSION :
      java version "1.4.1_01"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_01-b01)
      Java HotSpot(TM) Client VM (build 1.4.1_01-b01, mixed mode)

      FULL OPERATING SYSTEM VERSION :
      OS-independent

      ADDITIONAL OPERATING SYSTEMS :
      OS-independent

      A DESCRIPTION OF THE PROBLEM :
      Look at the source code of JEditorPane.getPreferredSize().
      It overrides preferred size calculation if the parent is a
      JViewport, but only then. This creates two bad problems:

      1) the behavior that this override is supposed to fix (see
      extensive comment in the source code for this method) still
      occurs if the JEditorPane is contained inside, say, a
      JPanel, which in turn, is contained in the JViewPort. This
      is a clearly a bug as the desired resizing does not work then.

      2) For all practical purposes, JEditorPanes can never be
      shrunk again after they have been enlarged!! To reproduce,
      see below.

      I suggest that the overridden code in
      JEditorPane.getPreferredSize() is a bad fix that creates
      more problems than it solves. I'm not an expert enough in
      Swing internals to suggest a good fix, but at this "fix" is
      clearly not a good one and needs to be undone.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1) run a JEditorPane inside a JScrollPane (scrollbars as
         needed) with just a bit of text so that the JScrollPane
         does not show its scrollbars.
      2) now enlarge the enclosing JFrame using the mouse.
      3) now shrink the enclosing JFrame to the approx.
         previous size. You will see the scrollbar still being
         there, because the JViewPort still has the large size
         and did not resize back to the previous size. Note that
         because there is only little text in the JEditorPane,
         it has no business remaining large.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      I expect the resizing behavior of JEditorPane to be
      identical to the resizing behavior of JTextArea. It is not
      -- in fact, it is clearly broken as it never gets smaller again.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
        From the implementation of JEditorPane.getPreferredSize() in the JDK:


          public Dimension getPreferredSize() {
      Dimension d = super.getPreferredSize();
      if (getParent() instanceof JViewport) { // <- This if shall not be here
      JViewport port = (JViewport)getParent();
      TextUI ui = getUI();
                  int prefWidth = d.width;
                  int prefHeight = d.height;
      if (! getScrollableTracksViewportWidth()) {
      int w = port.getWidth();
      Dimension min = ui.getMinimumSize(this);
      if (w != 0 && w < min.width) {
                          // Only adjust to min if we have a valid size
      ...
      ---------- END SOURCE ----------
      (Review ID: 180999)
      ======================================================================

            peterz Peter Zhelezniakov
            jkimsunw Jeffrey Kim (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: