aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Problem with BoxLayout and JTextField Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem with BoxLayout and JTextField" Watch "Problem with BoxLayout and JTextField" New topic
Author

Problem with BoxLayout and JTextField

kevin bennett
Greenhorn

Joined: Aug 22, 2001
Posts: 15
With the following code the JTextFields are enormous. What gives? I thought that the BoxLayout honored prefered sizes. Why are the JTextFields appearing differently? The enclosing class for this constructor is a Box. If I put it in a JPanel then the JTextFields appear at the normal size. Any help is greatly appreciated. Thx.
public CustomerInfoPane() {
super(BoxLayout.Y_AXIS);
//Initialize the text fields
initialize();
//Create Innner box
Box innerBox = new Box(BoxLayout.Y_AXIS);
innerBox.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
//Row 1
Box row1 = new Box(BoxLayout.X_AXIS);
//first name
row1.add(new JLabel("First Name:"));
row1.add(Box.createHorizontalStrut(5));
row1.add(firstName);
row1.add(Box.createHorizontalStrut(15));
//last name
row1.add(new JLabel("Last Name:"));
row1.add(Box.createHorizontalStrut(5));
row1.add(lastName);
row1.add(Box.createHorizontalStrut(35));
row1.add(Box.createGlue());
//Row 2
Box row2 = new Box(BoxLayout.X_AXIS);
//address line 1
row2.add(new JLabel("Address Line 1:"));
row2.add(Box.createHorizontalStrut(5));
row2.add(addressLine1);
row2.add(Box.createHorizontalStrut(25));
//day phone
row2.add(new JLabel("Daytime Phone:"));
row2.add(Box.createHorizontalStrut(5));
row2.add(dayPhone);
row2.add(Box.createGlue());
//Row 3
Box row3 = new Box(BoxLayout.X_AXIS);
//address line 2
row3.add(new JLabel("Address Line 2:"));
row3.add(Box.createHorizontalStrut(5));
row3.add(addressLine2);
row3.add(Box.createHorizontalStrut(25));
//eve phone
row3.add(new JLabel("Evening Phone:"));
row3.add(Box.createHorizontalStrut(5));
row3.add(evePhone);
row3.add(Box.createGlue());
//Row 4
Box row4 = new Box(BoxLayout.X_AXIS);
//city
row4.add(new JLabel("City:"));
row4.add(Box.createHorizontalStrut(5));
row4.add(city);
row4.add(Box.createHorizontalStrut(25));
//state
row4.add(new JLabel("State:"));
row4.add(Box.createHorizontalStrut(5));
row4.add(state);
row4.add(Box.createHorizontalStrut(15));
//zip
row4.add(new JLabel("Zip Code:"));
row4.add(Box.createHorizontalStrut(5));
row4.add(zip);
row4.add(Box.createHorizontalStrut(30));
//email
row4.add(new JLabel("Email:"));
row4.add(Box.createHorizontalStrut(5));
row4.add(email);
row4.add(Box.createGlue());
//Add rows to inner box
innerBox.add(row1);
innerBox.add(Box.createVerticalStrut(2));
innerBox.add(row2);
innerBox.add(Box.createVerticalStrut(2));
innerBox.add(row3);
innerBox.add(Box.createVerticalStrut(2));
innerBox.add(row4);
innerBox.add(Box.createGlue());
//Add the inner box to this panel
add(innerBox);
}
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Actually, the API for BoxLayout says -
"BoxLayout attempts to arrange components at their preferred widths (for left to right layout) or heights (for top to bottom layout). For a left to right layout, if not all the components are the same height, BoxLayout attempts to make all the components as high as the highest component."

I think that since the boxes you are adding the components to have an X_AXIS alignment they are honoring the preferred widths of the components only. Then, when you add each of those boxes to the outer Box with a Y_AXIS alignment the outer Box honors the preferred height of the inner Boxes, which is probably set to take up as much space as possible, so the entire size of the outer box is equally divided among the inner boxes... then, since the inner boxes are only honoring preferred width of their components, they make their components height the height of the largest component and this takes up as much possible space on the inside of the inner Boxes.

I would recommend that you use a JPanel with FlowLayout instead of Boxes for the inner Boxes, or change your layout to two vertical boxes rather than four horizontal boxes.


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem with BoxLayout and JTextField