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

JLabel in GroupLayout with Alignment.BASELINE starts with empty line

    Details

    • Type: Bug
    • Status: Open
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: 6u10
    • Fix Version/s: None
    • Component/s: client-libs

      Description

      FULL PRODUCT VERSION :
      java version "1.6.0_11"
      Java(TM) SE Runtime Environment (build 1.6.0_11-b03)
      Java HotSpot(TM) Client VM (build 11.0-b16, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      This appears to be a general Java Swing bug, not related to any OS or configuration (can reproduce on Mac OS X).

      A DESCRIPTION OF THE PROBLEM :
      When adding a JLabel to a GroupLayout'ed container, if the label contains a long text with line breaks through the <html> technique, and the JLabel is added with vertical Alignment.BASELINE, the text will not start at the top of the JLabel, but actually a line lower.

      If you set Alignment to LEADING, the text displays correctly. No other components are contained within the GroupLayout, so BASELINE should probably not have any effect at all (?!).

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile and run the attached source (javac GroupLayoutBug.java && java GroupLayoutBug)

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Text starting on top of the displayed JFrame
      ACTUAL -
      Text starts with an empty line.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.Color;

      import javax.swing.GroupLayout;
      import javax.swing.JFrame;
      import javax.swing.JLabel;
      import javax.swing.WindowConstants;
      import javax.swing.GroupLayout.Alignment;

      public class GroupLayoutBug {
        public static void main (String [] args) {
         JFrame window = new JFrame();
      window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
          
      GroupLayout layout = new GroupLayout(window.getContentPane());
      window.getContentPane().setLayout(layout);

      JLabel longText = new JLabel(
      "<html>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</html>");
      longText.setBackground(Color.RED);
      longText.setOpaque(true);

      layout.setHorizontalGroup(layout.createParallelGroup().addComponent(longText));
      layout.setVerticalGroup(layout.createSequentialGroup().addGroup(
      layout.createParallelGroup(Alignment.BASELINE).addComponent(longText)));

          window.setSize(800, 600);
          window.validate();
          window.setVisible(true);
        }
      }


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

      CUSTOMER SUBMITTED WORKAROUND :
      Add the JLabel in vertical alignment with Alignment.LEADING.

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              ryeung Roger Yeung (Inactive)
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Imported:
                Indexed: