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

Set Maximum Width of JEditorPane

    XMLWordPrintable

Details

    Description



      Name: sv35042 Date: 10/18/2002


      FULL PRODUCT VERSION :
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
      Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)


      I've seen this on Linux Solaris and Windows, though not
      always 100% consistently.

      A DESCRIPTION OF THE PROBLEM :
      There is no way to set the maximum width of a JEditorPane
      without setting the height as well. If you use
      setMaximumSize() with a large value for the height, some
      layout managers just ignore it (especially GridBagLayout) so
      you end up having to force everything through
      setPreferredSize() but then there's no (easy) way to
      persuade the component to behave as though it has a maximum
      width and a preferred height that is dependent on the
      content (which is effectively outside my control.)

      There isn't a (directly) containing JViewport because there
      are really several components (including several of these
      JEditorPanes) which are being scrolled as a piece.

      It would be *extremely* nice if it was possible to just
      directly specify the constraint in the one dimension and
      have the component work out what its natural size in the
      other should be, and I have trouble imagining that I am the
      only person who wants this feature.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        To reproduce this, create a JEditorPane (or JTextPane) and
      put it inside a JPanel (using a GridBagLayout.) Then use
      the JEditorPane's setContentType() to select "text/html" and
      setText() method to put some content with a long-ish
      paragraph in.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      What I'd like is to be able to control the width of the
      window, given that HTML is a series of paragraphs which can
      be line-broken between words, but what I get by default is a
      huge single line. Forcing people to scroll horizontally is
      not a good move in UI terms.

      REPRODUCIBILITY :
      This bug can be reproduced often.

      CUSTOMER WORKAROUND :
      final int MAXWIDTH = 500;
      JTextPane htmlPane = new JTextPane() {
         private Rectangle rect(javax.swing.text.Position p)
            throws javax.swing.text.BadLocationException
         {
            int off = p.getOffset();
            Rectangle r = modelToView(off>0 ? off-1 : off);
            return r;
         }
         public Dimension getPreferredSize() {
            try {
               Rectangle start =
                  rect(getDocument().getStartPosition());
               Rectangle end =
                  rect(getDocument().getEndPosition());
               if (start==null || end==null) {
                  return super.getPreferredSize();
               }
               int height = end.y + end.height - start.y + 4;
               return new Dimension(MAXWIDTH, height);
            } catch (javax.swing.text.BadLocationException e) {
               return super.getPreferredSize();
            }
         }
      };
      htmlPane.setPreferredSize(new Dimension(MAXWIDTH,32767));
      panel.horizExpand();
      panel.add(htmlPane);
      panel.doLayout();
      htmlPane.setPreferredSize(null);
      (Review ID: 158357)
      ======================================================================

      Attachments

        Activity

          People

            Unassigned Unassigned
            svioletsunw Scott Violet (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Imported:
              Indexed: