BoxLayout misunderstanding strut

boxlayout, java, swing

Solution

You are adding a `Box` strut to a `BoxLayout`.

As the javadoc states, `createHorizontalStrut(int width)`:

Creates an invisible, fixed-width component. In a horizontal box, you typically use this method to force a certain amount of space between two components. In a vertical box, you might use this method to force the box to be at least the specified width. The invisible component has no height unless excess space is available, in which case it takes its share of available space, just like any other component that has no maximum height.

As such, it is filling the height between your title `JLabel` and the bottom of the `JPanel`.

You might want to consider using `Box.createRigidArea(new Dimension(20, height))` instead, where height could be specified or set to the height of `labelPanel`.

Or, you could reconsider the layout for your `JPanel` - take a look at the visual guide.

For future reference, if you cannot make sense of your Swing layout, try putting adding a coloured `LineBorder` to the `JComponent`s you're unsure of. In this case, the `Box` struts are not `JComponent`s but `Component`s, so you'd have to put them into a `JPanel`, but this would at least have shown you what space each component was taking up in your top-level `JPanel`.

Problem

I'm prgramming a simple input diagram in Swing. I use boxLayout to create a simple GUI of user input. Problem is that creating a horizontal strut between the JPanel of all the labels and the JPanel of the JTextFields causes the whole panel to shift downwards (weird) this is the whole panel: ``` private JPanel secondCard() { //main panel. set the boxlayout secondCard = new JPanel(); secondCard.setLayout(new BoxLayout(secondCard,BoxLayout.Y_AXIS)); // create vertical strut for looks secondCard.add(Box.createVerticalStrut(20)); // create title. center it. JLabel title = new JLabel("Configure main network parameters "); title.setAlignmentX(CENTER_ALIGNMENT); secondCard.add(title); // create vertical strut for looks secondCard.add(Box.createVerticalStrut(20)); // create panel for the description labels JPanel labelPanel = new JPanel(); labelPanel.setLayout(new BoxLayout(labelPanel,BoxLayout.Y_AXIS)); labelPanel.setAlignmentX(LEFT_ALIGNMENT); JLabel inPut =new JLabel("number of inputs"); inPut.setAlignmentX(LEFT_ALIGNMENT); labelPanel.add(inPut); inPut =new JLabel("number of outputs"); inPut.setAlignmentX(LEFT_ALIGNMENT); labelPanel.add(inPut); inPut =new JLabel("number of layers"); inPut.setAlignmentX(LEFT_ALIGNMENT); labelPanel.add(inPut); JPanel textFieldPanel = new JPanel(); textFieldPanel.setLayout(new BoxLayout(textFieldPanel,BoxLayout.Y_AXIS)); textFieldPanel.setAlignmentX(LEFT_ALIGNMENT); JTextField inputTextField = new JTextField(); inputTextField.setAlignmentX(LEFT_ALIGNMENT); textFieldPanel.add(inputTextField); inputTextField.setMinimumSize(new Dimension(0,0)); inputTextField = new JTextField(); inputTextField.setAlignmentX(LEFT_ALIGNMENT); textFieldPanel.add(inputTextField); inputTextField.setMinimumSize(new Dimension(0,0)); inputTextField = new JTextField(); inputTextField.setAlignmentX(LEFT_ALIGNMENT); textFieldPanel.add(inputTextField); inputTextField.setMinimumSize(new Dimension(0,0)); textFieldPanel.setMaximumSize(new Dimension(50, labelPanel.getMaximumSize().height)); JPanel inputPanel = new JPanel(); inputPanel.setLayout(new BoxLayout(inputPanel,BoxLayout.X_AXIS)); inputPanel.setAlignmentX(CENTER_ALIGNMENT); inputPanel.add(labelPanel); //this is the problem strut!! it causes inputPanel to shift downwards inputPanel.add(Box.createHorizontalStrut(20)); inputPanel.add(textFieldPanel); secondCard.add(inputPanel); return secondCard; } ``` without the strut it looks like: With strut it looks like (I know I suck at picture editing):

Original source