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

BoxLayout misaligns JLabel when used together with JComboBox

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 1.3.0
    • client-libs
    • generic, x86
    • generic, windows_nt



      Name: jk109818 Date: 07/11/2000


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

      When you place a JLabel and a JTextField in a vertical Box, the JLabel is (as
      expected) aligned to the left. This always seemed to be default behaviour and
      is just fine. This also works correctly under JDK 1.3.

      Now if you put a JLabel and a JComboBox together in a vertical Box, the
      alignment of the JLabel is somewhere at 0.6. I only notice this when the JLabel
      is in the Box with a JComboBox. So far I have no clue why this would be so. I
      also have not been able to find any workarounds.
      JLabel.setHorizontalAlignment() does not have any effect on the label.

      This behaviour can be easily reproduced under JDK 1.3. JDK 1.2 and 1.1 (Swing
      1.1.1) do not show this behaviour.

      Here is a suitable test case:


      // Imports
      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;



      public class TestApp extends JFrame
      {
          /**
           * Default constructor.
           */
          public TestApp() {
              super("TestApp");

              this.addWindowListener(new WindowAdapter() {
                  public void windowClosing(WindowEvent ev) {
                      System.exit(0);
                  }
              });

              JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true,
                                                new TopPanel(), new BottomPanel());

              this.getContentPane().setLayout(new BorderLayout());
              this.getContentPane().add(split, BorderLayout.CENTER);

              this.setSize(200, 100);
              this.setVisible(true);
          }


          /**
           * Application startup method.
           */
          public static void main(String[] args) {
              new TestApp();
          }


          /**
           * The panel at the top of the splitpane.
           */
          private class TopPanel extends JPanel
          {
              /**
               * Default constructor.
               */
              public TopPanel() {
                  super(new BorderLayout());

                  JLabel label = new JLabel("JLabel");

                  // None of the following 2 commands seem to have any effect
                  // on the alignment of the label.
                  //
                  //label.setHorizontalAlignment(JLabel.LEFT);
                  //label.setHorizontalTextPosition(JLabel.LEFT);

                  JComboBox combo = new JComboBox(new Object[] {"JComboBox"});

                  Box box = new Box(BoxLayout.Y_AXIS);
                  box.add(label);
                  box.add(combo);

                  this.add(box, BorderLayout.CENTER);
              }
          }


          /**
           * The panel at the bottom of the splitpane.
           */
          private class BottomPanel extends JPanel
          {
              /**
               * Default constructor.
               */
              public BottomPanel() {
                  super(new BorderLayout());

                  JLabel label = new JLabel("JLabel");
                  JTextField field = new JTextField("JTextField");

                  Box box = new Box(BoxLayout.Y_AXIS);
                  box.add(label);
                  box.add(field);

                  this.add(box, BorderLayout.CENTER);
              }
          }
      }
      (Review ID: 107064)
      ======================================================================

      Name: yyT116575 Date: 08/20/2001


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

      There seems to be some semantic confusion in JLable between the Swing and
      AWT versions of getAlignmentX(). If you create a JLabel with a central
      alignment (new JLabel("text", JLabel.CENTER)) and then call
      getAlignmentX(), the returned value will be 0.0. (Because the SwingConstant
      CENTER has been cast to a float in the JLabel contructor, and passed in to
      setAlignmentX())

      However, a Component alignment of 0.0 is defined as being aligned to the
      left. This can cause confusion in a number of areas - my particular example
      occurred in a BoxLayout, where the call to
      SizeRequirements.getAlignedSizeRequirements() returned a greater width than
      necessary, becasue it thought that a JLabel was left aligned instead of
      centre aligned.

      The sample code produces a box layout that is wider than necessary because
      of the confusion over the semantics of the getAlignmentX() method. If you
      uncomment the explicit call to setAlignmentX() then the box will be
      correctly size to fit the components.

       
      // Imports
      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;

      public class TestApp extends JFrame {
        /**
         * Default constructor.
         */
        public TestApp() {
          super("TestApp");

          this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent ev) {
              System.exit(0);
            }
          });
          this.getContentPane().add(new MyPanel());
          this.pack();
          System.out.println("size = " + getSize());
              
          this.setVisible(true);
        }

        /**
         * Application startup method.
         */
        public static void main(String[] args) {
          new TestApp();
        }

        private class MyPanel extends JPanel {
          /**
           * Default constructor.
           */
          public MyPanel() {
            this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
            String txt = "This is a text label with a lot of text in it. It goes on "
                + "and on and on for what is really quite a long time indeed.";
            JLabel label = new JLabel(txt, JLabel.CENTER);

            System.out.println("horizontal alignment = " + label.getAlignmentX());

            // By uncommenting the following lines, the total size of the window
            // is reduced, and the label fits neatly into the window contents
            //label.setAlignmentX(0.5f);
            //System.out.println("horizontal alignment = " + label.getAlignmentX());
                  

            this.add(label);
            this.add(new MyInnerPanel());
          }
        }

        private class MyInnerPanel extends JPanel {
          public MyInnerPanel() {
            this.setBorder(BorderFactory.createTitledBorder("MyInnerPanel"));
          }

          public Dimension getPreferredSize() {
            return new Dimension(640, 480);
          }
        }

      }
      (Review ID: 130311)
      ======================================================================

            dmcduffisunw Dale Mcduffie (Inactive)
            jkimsunw Jeffrey Kim (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: