0

I am trying to create panel with ability to enter categories and entires.

In the picture you can see JTextField with a JButton at the bottom of the window.

enter image description here

If the user adds a category, a new row with components will appear in the upper panel.

The problem is, new added row doesn't wrap, it takes all the space.

How could I force every added row to wrap it's height (like below)?

enter image description here

Here is my code:

.java:

public class Dinner {
    private JPanel panelRoot;
    private JTextField textFieldCategory;
    private JButton buttonAddCategory;
    private JPanel panelInput;
    private JPanel panelControl;

    public static void main(String[] args) {
        new Dinner().doStuff();
    }

    private void doStuff() {
        panelInput.setLayout(new BoxLayout(panelInput, BoxLayout.Y_AXIS));

        buttonAddCategory.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JPanel jPanel = new JPanel();
                jPanel.setLayout(new BoxLayout(jPanel, BoxLayout.X_AXIS));

                JTextField jTextField = new JTextField();
                jPanel.add(jTextField);
                JButton jButton = new JButton("Add entry");
                jPanel.add(jButton);
                panelInput.add(jPanel);

                panelRoot.revalidate();
            }
        });

        showWindow();
    }

    private void showWindow() {
        JFrame frame = new JFrame("Dinner");
        frame.setContentPane(panelRoot);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setMinimumSize(new Dimension(680, 0));
        frame.pack();
        frame.setVisible(true);
    }
}

.form:

<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="xxx.Dinner">
  <grid id="27dc6" binding="panelRoot" layout-manager="BorderLayout" hgap="0" vgap="0">
    <constraints>
      <xy x="20" y="20" width="500" height="400"/>
    </constraints>
    <properties/>
    <border type="none"/>
    <children>
      <grid id="57444" binding="panelInput" layout-manager="BorderLayout" hgap="0" vgap="0">
        <constraints border-constraint="Center"/>
        <properties/>
        <border type="none"/>
        <children/>
      </grid>
      <grid id="59d09" binding="panelControl" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
        <margin top="0" left="0" bottom="0" right="0"/>
        <constraints border-constraint="South"/>
        <properties/>
        <border type="none"/>
        <children>
          <component id="1076b" class="javax.swing.JTextField" binding="textFieldCategory">
            <constraints>
              <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
                <preferred-size width="150" height="-1"/>
              </grid>
            </constraints>
            <properties/>
          </component>
          <component id="d75cb" class="javax.swing.JButton" binding="buttonAddCategory">
            <constraints>
              <grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
            </constraints>
            <properties>
              <text value="Add Category"/>
            </properties>
          </component>
        </children>
      </grid>
    </children>
  </grid>
</form>
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
deadfish
  • 11,996
  • 12
  • 87
  • 136
  • It does not seem to be 'wrap' that you want, but to have the components not stretched vertically. I'm also guessing you want them compacted to the top of the container and to be able to add more after the GUI is visible (based on user actions). Are my summary and guesses correct? – Andrew Thompson Jan 15 '18 at 06:49

1 Answers1

0
  1. Add container JPanel of BorderLayout type
  2. Add each new BoxLayout JPanel to BorderLayout.PAGE_START;

Duplicate of Keep BoxLayout From Expanding Children

All credit to author.

  • *"Add each new BoxLayout JPanel to BorderLayout.PAGE_START;"* Each position (e.g. `PAGE_START`) in a `BorderLayout` supports exactly **one** child component. If more than one is required, add one panel (using an appropriate layout) as the container for all the others. – Andrew Thompson Jan 15 '18 at 06:45