I am trying out GroupLayout for the first time. When I just added the components to the containing JPanel, not all components were showing up, but the JComboBox was one of the ones that did show up. After instantiating the components, I used the addComponent method of the GroupLayout API, but when I run the program, I get an InvalidStateException on the JComboBox.
Here is the code
public class PersonalInfo extends JPanel {
private JLabel jLabel1;
private JTextField firstName;
private JTextField lastName;
private JLabel jLabel2;
private JTextField address1;
private JTextField address2;
private JCheckBox toggleAddress3;
private JTextField address3;
private JTextField city;
private JTextField state;
private JTextField postalCode;
private JLabel jLabel3;
private JComboBox<String> gender;
private JLabel jLabel4;
// private Date dateOfBirth;
/**
* Creates new form PersonalInfo
*/
public PersonalInfo() {
initComponents();
setPreferredSize(new Dimension(400, 800));
validate();
}
private void initComponents() {
GroupLayout groupLayout = new GroupLayout(this);
setLayout(groupLayout);
groupLayout.setAutoCreateGaps(true);
groupLayout.setAutoCreateContainerGaps(true);
jLabel1 = new JLabel("Name");
jLabel1.setHorizontalAlignment(SwingConstants.RIGHT);
firstName = new JTextField(50);
firstName.setToolTipText("First Name");
lastName = new JTextField(50);
lastName.setToolTipText("Last Name");
jLabel2 = new JLabel("Address");
jLabel2.setHorizontalAlignment(SwingConstants.RIGHT);
address1 = new JTextField(50);
address1.setToolTipText("Address 1");
address2 = new JTextField(50);
address2.setToolTipText("Address 2");
add(address2);
toggleAddress3 = new javax.swing.JCheckBox();
toggleAddress3.setText("Show third address");
toggleAddress3.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
setToggle();
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
});
address3 = new JTextField(50);
address3.setToolTipText("Address 3");
address3.setVisible(false);
city = new JTextField(50);
city.setToolTipText("City");
state = new JTextField(10);
state.setToolTipText("State");
postalCode = new JTextField(25);
postalCode.setToolTipText("Postal Code");
jLabel3 = new JLabel("Gender");
jLabel3.setHorizontalAlignment(SwingConstants.RIGHT);
add(jLabel3);
String[] items = {"", "Male", "Female"};
gender = new JComboBox<String>(items);
gender.setToolTipText("Gender");
jLabel4 = new JLabel("Date of Birth");
jLabel4.setHorizontalAlignment(SwingConstants.RIGHT);
groupLayout.setHorizontalGroup(groupLayout.createParallelGroup()
.addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(firstName)
.addComponent(lastName))
.addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(address1))
.addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(toggleAddress3)
.addComponent(address2))
.addComponent(address3)
.addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(city)
.addComponent(state)
.addComponent(postalCode))
.addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(gender))
.addComponent(jLabel4)
);
groupLayout.setVerticalGroup(groupLayout.createParallelGroup()
.addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.TRAILING))
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jLabel3)
.addComponent(jLabel4));
}
private void setToggle() {
if (toggleAddress3.isSelected()) {
address3.setVisible(false);
} else {
address3.setVisible(true);
}
repaint();
}
}
Any help with this is greatly appreciated.
Scratching my head,
Tom Magaro
Refer to How to Use GroupLayout.
You are not adding all your components to both the horizontal group and the vertical group and hence the exception you are getting.
Actually, this is one time where I would suggest actually using the GUI builder of your IDE since
GroupLayoutwas created specifically for GUI builders. After you create your GUI using the builder, look at the [java] code that it generated in order to try to understand howGroupLayoutworks.I tried to guess how you want your
PersonalInfopanel to look and created one using Eclipse WindowBuilder. For what it's worth, here is the generated code.Note that I added a
JSpinnerfor the Date of Birth field.Here is a screen capture.