I Have some part of view which show text fields in JOptionPane
. And there is a button "other". After click on it I need JOptionPane
repaint itself and show some hidden text fields.
I use GridLayout for my text fields. I tried revalidate()
and repaint()
methods in the button listener but they do nothing changes.
The validate()
method works but the size of all components is too small. Seems wrong resizing in the grid panel. In my original code some components are just hiding. I didn't setPrefferedSize()
or getPrefferedSize()
because I read it is wrong, but can't figure out how to make right resizing after.
I take my code and did simple as I can, just a part of view.
public class AddView {
private JFrame parentFrame;
private JPanel addPanel;
private JPanel gd;
private JLabel nameLabel;
private JLabel surName;
private JLabel skype;
private JTextField nameTField;
private JTextField surNameTField;
private JTextField skypeTField;
private JButton otherButton;
public AddView(JFrame parent) {
this.parentFrame = parent;
initComponents();
}
private void initComponents() {
addPanel = new JPanel(new BorderLayout());
gd = new JPanel(new GridLayout(2, 2, 0, 5));
nameLabel = new JLabel("Name");
surName = new JLabel("Surname");
skype = new JLabel("Skype");
nameTField = new JTextField();
surNameTField = new JTextField();
skypeTField = new JTextField();
otherButton = new JButton("Other");
gd.add(nameLabel);
gd.add(nameTField);
gd.add(surName);
gd.add(surNameTField);
addPanel.add(gd, BorderLayout.CENTER);
addPanel.add(otherButton, BorderLayout.SOUTH);
otherButton.addActionListener(new OtherFieldsAction());
}
public int showAddPane() {
return JOptionPane.showConfirmDialog(parentFrame, addPanel, "Add Contact", JOptionPane.OK_CANCEL_OPTION);
}
private class OtherFieldsAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
gd.setLayout(new GridLayout(3, 2, 0, 5));
gd.add(skype);
gd.add(skypeTField);
// gd.revalidate(); not work
// gd.repaint();
gd.validate();
}
}
}
This is JOptionPane
When I click "other" button I have this result
But I need to get it with right resize like this
Can you give an advice about how to make right repaint.