I'm using a JLabel to notify the user if there's an error with his/her input inside a JTextField.
If there's a problem, I'm setting the text of the JLabel to "invalid input". However, when the text in the label is set, it slightly 'pushes' the rest of the layout a little. I want everything to stay in it's place. Any ideas?
public Dialog(Window owner){
widthL = new JLabel("Width: ");
heightL = new JLabel("Height: ");
widthF = new JTextField(5);
heightF = new JTextField(5);
apply = new JButton("Apply");
error = new JLabel("");
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10,10,10,10);
gbc.gridy = 1;
gbc.gridx = 1;
add(widthL,gbc);
gbc.gridx = 2;
add(widthF,gbc);
gbc.gridy = 2;
gbc.gridx = 1;
add(heightL,gbc);
gbc.gridx = 2;
add(heightF,gbc);
gbc.gridy = 1;
gbc.gridx = 3;
gbc.gridheight = 2;
add(apply,gbc);
gbc.gridheight = 1;
gbc.gridy = 3;
gbc.gridx = 2;
add(error,gbc);
apply.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
int width,height;
try{
width = Integer.parseInt(widthF.getText());
height = Integer.parseInt(heightF.getText());
}catch(NumberFormatException ex){
error.setText("Invalid input");
}
}