JDK-4143672 : BoxLayout not giving enough space to non-panel components
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.2.0
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: solaris_2.5.1
  • CPU: sparc
  • Submitted: 1998-05-28
  • Updated: 2001-11-09
  • Resolved: 2001-11-09
Related Reports
Relates :  
Description

Name: bk70084			Date: 05/28/98


If BoxLayout is used to layout JPanel which contains
another JPanel the layout of other components is not
done correctly.

The layout that I was expecting was that "label1", "panel1" and "label4" would extend right across
the window horizontally (right - left justified) and take-up approximately equal space vertically.

The layout I see is "label1" and "label4" starting half way across the window and extend to the right
limit of the window (aligned with "label3"). While "panel1" does extend the full width of the window.


The following code demonstrates the problem:

import java.awt.*;
import java.awt.event.*;
import java.awt.swing.*;
import java.awt.swing.border.*;

public class Test extends JPanel
{
    public Test()
    {
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        JLabel label1 = new JLabel("Label1", SwingConstants.CENTER);
        label1.setBorder(new TitledBorder(LineBorder.createBlackLineBorder(), "Label1"));
        label1.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
        add(label1);

        JPanel panel1 = new JPanel();
        panel1.setLayout(new BoxLayout(panel1, BoxLayout.X_AXIS));
        panel1.setBorder(new TitledBorder(LineBorder.createBlackLineBorder(), "Panel1"));
        panel1.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
        add(panel1);
        
        JLabel label2 = new JLabel("Label2", SwingConstants.CENTER);
        label2.setBorder(new TitledBorder(LineBorder.createBlackLineBorder(), "Label2"));
        label2.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
        panel1.add(label2);

        JLabel label3 = new JLabel("Label3", SwingConstants.CENTER);
        label3.setBorder(new TitledBorder(LineBorder.createBlackLineBorder(), "Label3"));
        label3.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
        panel1.add(label3);

        JLabel label4 = new JLabel("Label4", SwingConstants.CENTER);
        label4.setBorder(new TitledBorder(LineBorder.createBlackLineBorder(), "Label4"));
        label4.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
        add(label4);
    }

    public static void main(String args[])
    {
        try
        {
            JFrame frame = new JFrame("Test");

            WindowListener l = new WindowAdapter()
            {
                public void windowClosing(WindowEvent e)
                {
                    System.exit(0);
                }
            };
            frame.addWindowListener(l);

            Test test = new Test();

            frame.getContentPane().add(BorderLayout.CENTER, test);

	    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            frame.setSize(screenSize.width / 2, screenSize.height / 2);
            frame.setLocation(screenSize.width / 4, screenSize.height / 4);

            frame.show();
        }
        catch (Exception e)
        {
            System.err.println("ERROR: " + e);
            e.printStackTrace(System.out);
        }
    }
}
(Review ID: 28674)
======================================================================

Comments
EVALUATION Name: keR10081 Date: 10/30/2001 Everything works as said in docs for BoxLayout: " Similarly, for a vertical layout, BoxLayout attempts to make all components in the column as wide as the widest component. If that fails, it aligns them horizontally according to their X alignments. For PAGE_AXIS layout, horizontal alignment is done based on the leading edge of the component. In other words, an X alignment value of 0.0 means the left edge of a component if the container's ComponentOrientation is left to right and it means the right edge of the component otherwise. " Due to the huge set maximum size and the fact that by default panels have X alignment set to 0.5, labels (which by default have X alignment 0.0) get panel's X alignment, by so getting "incorrect" look. See workaround. This is probably not a bug. ###@###.### ======================================================================
11-06-2004

WORK AROUND Name: keR10081 Date: 10/30/2001 There are two ways to make testcase work: 1. panel1.setAlignmentX(0.5f) 2. don't call setMaximumSize with unreasonably huge values, like Short.MAX_VALUE ======================================================================
11-06-2004