2

I use GridBagConstraints to update my layout, when a button have been clicked, some input fields will appear.

private class EventListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            input_panel.removeAll();
               String str = e.getActionCommand();
               System.out.print(str);

               JLabel jl = new JLabel("Label ");
            // Create constraints
               GridBagConstraints textFieldConstraints = new GridBagConstraints();
               GridBagConstraints labelConstraints = new GridBagConstraints();

               labelConstraints.gridx = 0;
               labelConstraints.gridy = 0;

               input_panel.add(jl,textFieldConstraints);
        }
    }

This function successfully runs, however, there is a strange problem, when I click the button, it do update, but not show immediately, I must need to resize the window to see it. What happens?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
panda
  • 1,344
  • 3
  • 14
  • 35

1 Answers1

4
input_panel.add(jl,textFieldConstraints);
input_panel.revalidate(); //try to add this
input_panel.repaint(); // and this
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Nikolay Kuznetsov
  • 9,467
  • 12
  • 55
  • 101