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

[macosx] Swing components don't align correctly when using GroupLayout

    XMLWordPrintable

Details

    Description

      FULL PRODUCT VERSION :
      java version "1.7.0_45"
      Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
      Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Mac OS X Mavericks (same results on Snow Leopard, probably all.)

      A DESCRIPTION OF THE PROBLEM :
      Using GroupLayout, an alignment of LEADING does not result in the components visually aligning to the left (assuming LTR orientation.)

      Screenshots of the issue can be found here:
      http://stackoverflow.com/questions/21627970/


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Components should align such that you can draw a vertical line down the screen and touch the leftmost border of each component.

      ACTUAL -
      Buttons indent out several pixels to the right. Other components all have their own pixel amount which they jut out. Only basic components like JPanel reliably align.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javax.swing.BorderFactory;
      import javax.swing.GroupLayout;
      import javax.swing.JButton;
      import javax.swing.JComponent;
      import javax.swing.JFrame;
      import javax.swing.JPanel;
      import javax.swing.LayoutStyle;
      import javax.swing.SwingUtilities;
      import javax.swing.WindowConstants;
      import java.awt.BorderLayout;
      import java.awt.Color;
      import java.awt.Dimension;

      public class ButtonVisualMarginTest implements Runnable {
          public static void main(String[] args) {
              SwingUtilities.invokeLater(new ButtonVisualMarginTest());
          }

          @Override
          public void run() {
              LayoutStyle.setInstance(new AquaLayoutStyle());

              JComponent reference = new JComponent() {};
              reference.setName("reference component");
              reference.setPreferredSize(new Dimension(200, 50));
              reference.setMaximumSize(new Dimension(200, 50));
              reference.setBorder(BorderFactory.createLineBorder(Color.RED));

      // JButton reference = new JButton("Another button");
      // reference.setName("another button");
      // reference.setFocusable(false);

              JButton button = new JButton("Text only");
              button.setName("button");
              button.setFocusable(false);

              JPanel container = new JPanel();
              container.setName("container");
              GroupLayout layout = new GroupLayout(container);
              layout.setAutoCreateGaps(true);
              layout.setAutoCreateContainerGaps(true);
              container.setLayout(layout);
              container.setBackground(new Color(192, 192, 255));
              container.setOpaque(true);

              layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                  .addComponent(reference)
                  .addComponent(button));

              layout.setVerticalGroup(layout.createSequentialGroup()
                  .addComponent(reference)
                  .addComponent(button));

              JFrame frame = new JFrame("Test");
              frame.setLayout(new BorderLayout());
              frame.add(container, BorderLayout.CENTER);
              frame.setSize(400, 300);
              frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
              frame.setVisible(true);

          }
      }

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

      Attachments

        Activity

          People

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated: