Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with BoxLayout and JTextField

 
kevin bennett
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic