Search code examples
javaswingjpanelframe

Remove Jframe panels


JFrame frame1 = new JFrame("frame");
frame1.setVisible(true);
frame1.setPreferredSize(new Dimension(800, 600));
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel Panel1 = new JPanel();
JPanel Panel2 = new JPanel();
frame1.add(Panel1,BorderLayout.SOUTH);
frame1.add(Panel2,BorderLayout.North);

How do i do something like this that if something happens the frame is blank

if(SOMETHINGHAPPENS)
   {
     //remove all panels from frame 1 so i have a blank frame 
    //now i want to add some new panels
   }

Solution

  • Simple answer, don't.

    Instead, use a CardLayout on the JFrame

    This will allow you to setup a series of "views" which you can switch between as needed