0

Is there a way to give a JButton an Action Listener that will change the contents of the JPanel the button is in (and deleting the button in the process). I have this code right now but it just makes the code freeze up.

public class CL extends Frame implements WindowListener, ActionListener
{
    //Main method calls initializer (and sets size and visibility)

    //Initializer adds a panel to the frame and a button that calls the action Listener
    public void actionPerformed(ActionEvent event) 
    {
        //Clear the JPanel (That the button is in)
        panel.removeAll();
        //Add new contents to the panel
        JLabel label = new JLabel("This is a new label")
        panel.add(label);
    }
}

This doesn't throw any errors so... is there an error here or am I approaching this completely wrong? Thanks in advance for any help

Bill Bllson
  • 179
  • 1
  • 1
  • 14
  • 1
    Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). – Andrew Thompson Jun 05 '16 at 22:13
  • 1) `public class CL extends Frame ..` should be `public class CL extends JFrame ..` though `CL` should be a sensible name. 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Jun 05 '16 at 22:16
  • @AndrewThompson Ah. Thanks, and I'll try to do that in the future. – Bill Bllson Jun 05 '16 at 22:21

0 Answers0