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
  • 166,747
  • 40
  • 210
  • 420
panda
  • 1,334
  • 3
  • 14
  • 33

1 Answers1

4
input_panel.add(jl,textFieldConstraints);
input_panel.revalidate(); //try to add this
input_panel.repaint(); // and this
Hovercraft Full Of Eels
  • 280,125
  • 25
  • 247
  • 360
Nikolay Kuznetsov
  • 9,217
  • 12
  • 53
  • 97