Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BoxLayout misunderstanding strut

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: enter image description here

With strut it looks like (I know I suck at picture editing):

enter image description here

like image 607
hasdrubal Avatar asked Dec 28 '22 00:12

hasdrubal


2 Answers

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 JComponents you're unsure of. In this case, the Box struts are not JComponents but Components, 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.

like image 72
amaidment Avatar answered Jan 08 '23 01:01

amaidment


  • use Cardlayout for wizard logics

  • put JLabel(Configure ...., JLabel.CENTER) to the BorderLayout.NORTH

  • put JPanel with JButtons to the BorderLayout.SOUTH

  • put JPanel with SpringLayout, GridLayout, or GridBagLayout to the BorderLayout.CENTER

  • Top-Level Container have got implemened BorderLayout by default, then there no reason to re_define BorderLayout

  • above mentioned steps are called NestedLayout

  • alternative are put all JComponents by using GridBagLayout, SpringLayout or todays MigLayout to the one JPanel, but why bothering

like image 30
mKorbel Avatar answered Jan 08 '23 00:01

mKorbel