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

JLabel with HTML text uses improper line spacing.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.4.0
    • client-libs



      Name: ddT132432 Date: 01/15/2002


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

      java version "1.3.1"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24)
      Java HotSpot(TM) Client VM (build 1.3.1-b24, mixed mode)


      FULL OPERATING SYSTEM VERSION :
      Microsoft Windows 2000 [Version 5.00.2195]
      Service Pack 1



      A DESCRIPTION OF THE PROBLEM :
      HTMLJLabelBug.java

      JLabel with HTML text uses improper line spacing.

        When using html to specify the text for a JLabel, the last
      line of a paragraph often gets set apart from the rest of
      the text. It actually gets about two to five pixels of
      extra leading.
        This is very annoying, since I often use the spacing of
      the lines to guide the user through the text. If the JLabel
      inserts extra leading, it makes the user think I'm trying
      to emphasize the last line. It's also ugly. The amount of
      extra leading varies from one font to font and from size to
      size, and may well very from platform to platform.
        The effect is most noticable when using small fonts. The
      problem usually goes away completely if the text has
      multiple paragraphs. (This isn't always a practical
      solution.) So inserting a <p> in the text will fix the
      problem, although inserting a <br> doesn't. (See below for
      where this workaround fails.)
        To see the problem, run this program. You may need to
      expand the window to see all the text. (If your window
      isn't big enough, it might allocate way too much space for
      each paragraph. This could be a different bug. If you
      can't expand the window enough to see all the text, try
      commenting out some of the entries in the "faces" array in
      the init() method.)
        The problem varies according to the look and feel. I
      tested it on Windows
      only, under both the Metal and Windows look and feel. This
      table show where the bug shows up.

              Metal Windows
      1.3.1 Yes Yes
      1.4b3 Yes No Bug

        The workaround is to start the text with a <p>. This
      doesn't always work. This table shows where the workaround
      works:

              Metal Windows
      1.3.1 Yes Yes
      1.4b3 Fails Yes

        For JDK 1.4b3, under Metal, not only does the workaround
      fail, but the text all shows up in boldface.

        I haven't test this under other platforms or
      looks-and-feels.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the program given in the description.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      Each paragraph of text should show up with constant line
      spacing. Instead, the last line of each paragraph is
      often given a few points of extra leading.

      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javax.swing.*;
      import javax.swing.text.*;
      import javax.swing.event.*;
      import java.awt.*;
      import java.awt.event.*;
      import java.util.*;
      import java.io.*;
      import java.text.*;

      public class HTMLJLabelBug extends JPanel
      {
        public static void main(String[] args)
        {
          if (args.length > 0)
            try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      }
            catch (Exception lfex1) { }
          JFrame mf = new JFrame("HTMLJLabelBug");
          WindowAdapter wa = new WindowAdapter()
          {
            public void windowClosing(WindowEvent we)
            {
              System.exit(0);
            }
          };
          mf.addWindowListener(wa);
          mf.getContentPane().add(new HTMLJLabelBug());
          mf.setBounds(10, 10, 400, 600);
          mf.show();
        }
        
        /** Creates new HTMLJLabelBug */
        public HTMLJLabelBug()
        {
          super(new BorderLayout());
          init();
        }
        
        private void init()
        {
          // For some reason, if the window is too small, the whole thing takes up
          // way too much space to fit on the screen. This may be a different bug,
          // or it may be a different manifestation of the same bug.
          String[] faces = {
            "Courier New",
            "Arial",
            "Times New Roman",
            "Default",
            "Serif",
            "Sans",
            "Missing",
          };
          
          JPanel pnl = new JPanel();
      // pnl.setLayout(new BoxLayout(pnl, BoxLayout.Y_AXIS));
          pnl.setLayout(new GridLayout(0, 1));
          add(pnl, BorderLayout.NORTH);
          for (int ii=0; ii<faces.length; ++ii)
          {
            JLabel lbl = new JLabel(getHtmlText(faces[ii]));
            pnl.add(lbl);
          }
        }
        
        // Here's the workaround. If you start the paragraph with the HTML paragraph
        // designator, the problem goes away.
        private static String wkrnd = ""; // workaround text
        
        private String getHtmlText(String face)
        {
          // Uncomment this line to fix the problem. The fix fails in JDK 1.4b3,
          // with the Metal look and feel.
      // wkrnd = "<p>";
          String htmlText = "<html><body><font face='" + face + "' size=-1>" + wkrnd + "&nbsp;&nbsp;<b>This</b> is a multi-line label,using the "
          // change the <br> to a <p> to make the problem go away.
          // remove the <br> and the problem will still be there.
          + face + " font. Notice that the last line may be separated from the other lines by a slightly larger space than normal. <br>This extra space is usually about 2 to 5 pixels high.</font></body></html>";
          return htmlText;
        }
      }

      ---------- END SOURCE ----------

      CUSTOMER WORKAROUND :
      Start the text with a <p>. This won't insert a blank line
      before the paragraph, so it's perfectly save. It fails under
      Metal with JDK 1.4b3
      (Review ID: 138326)
      ======================================================================

            peterz Peter Zhelezniakov
            ddressersunw Daniel Dresser (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: