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.
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)?
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>