0

I have a JPanel to which when a button is pressed I want to add a new JLabel and JTextField too. However, I can't seem to get it working.

Is there an issue with my ActionListener, and if not, how could this be achieved?

JPanel south = new JPanel();
JButton add = new JButton("Add");
ActionListener addListener = new ActionListener() {

     @Override
     public void actionPerformed(ActionEvent e) {
           JLabel mL = new JLabel("MOD: ");
           mR.add(mL);
           JTextField mM = new JTextField(10);
           mR.add(mM);
           mR.repaint();

     }

};
add.addActionListener(addListener);
south.add(add);
add(south, BorderLayout.NORTH);

The layout of the mR panel is a grid layout set to allow multiple rows and two columns.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
mino
  • 6,978
  • 21
  • 62
  • 75

2 Answers2

4

Call mR.revalidate() before repaint();

StanislavL
  • 56,971
  • 9
  • 68
  • 98
0

See my answer on a previous SO question for some sample code which dynamically adds a component to a container

Community
  • 1
  • 1
Robin
  • 36,233
  • 5
  • 47
  • 99