I have a JMenuItem called newMI, in a class that extends JFrame. I want to add Swing components to my JFrame when I click the JMenuItem. For testing purposes, I am trying to add a JPanel and setting the background color of the JPanel to red.
Here is my ActionListener:
newMI.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JPanel p = new JPanel();
p.setBackground(Color.red);
add(p);
}
}
However this isn't working. I can change the background color of the JPanel if I added it to the JFrame during the initialization of the other Swing components. But I can't add Swing components to the JFrame directly inside of an ActionListener. Can somebody please help?
Many thanks.