1
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
   }
braX
  • 11,506
  • 5
  • 20
  • 33
theForgottenCoder
  • 145
  • 1
  • 1
  • 11
  • http://stackoverflow.com/questions/2501861/how-can-i-remove-a-jpanel-from-a-jframe may help. In other words, use `frame1.remove()`. – Nico Mar 26 '14 at 03:23
  • @MadProgrammer Thanks. Java 7, it does: http://docs.oracle.com/javase/7/docs/api/javax/swing/JFrame.html#remove(java.awt.Component) I am not sure about previous versions though. – Nico Mar 26 '14 at 03:28
  • @NicolásCarlo Yeah, it's `removeAll` that does freakish stuff – MadProgrammer Mar 26 '14 at 03:29
  • To remove all panels, see this: http://stackoverflow.com/questions/9347076/how-to-remove-all-components-from-a-jframe-in-java – Kavka Mar 26 '14 at 03:32

2 Answers2

4

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

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • trying to look into cardlayouts will mark as right answer if it all worksout thanks for the suggestion – theForgottenCoder Mar 26 '14 at 03:32
  • @MadProgrammer can u please assist me with how to implement cards into this i followed a online tutorial doesn't seem to function tho – theForgottenCoder Mar 26 '14 at 03:44
  • @lilz4life [example](http://stackoverflow.com/questions/17477891/how-do-i-use-cardlayout-for-my-java-program-for-login-and-menu-items/17479575#17479575) – MadProgrammer Mar 26 '14 at 03:47
  • @MadProgrammer in this example they use card layouts to navigate within single panels although my frame displays multiple panels for exmaple at the moment i have 2 panels then when something happens i want to display multiple other panels do – theForgottenCoder Mar 26 '14 at 03:59
  • @lilz4life And? Make a view, which contains multiple panels. This will act as a single view from the perspective of the `CardLayout`, but will display multiple different components. The concept of what you were originally trying to do hasn't changed, the method by which you achieve it has... – MadProgrammer Mar 26 '14 at 04:02
  • OOOO i think im on the right track instead of adding them to a view i added all the others to a single panel is that okay? or should i look into views – theForgottenCoder Mar 26 '14 at 04:05
  • Add as much or as little as you need. A individual "card" or "view" is just a container, which you can switch to as you need... – MadProgrammer Mar 26 '14 at 04:06
  • WOHOOOO WORKED <3 THANKS MAN appreciate it – theForgottenCoder Mar 26 '14 at 04:32
0

Try this. Jframe.remove(JPanel). Then call JFrame.pack() , and possibly JFrame.repaint() in order to refresh the graphics if necessary.

JPanel extends JComponent, so it should work just fine. Remember to substitute JFrame and JPanel in the code above with the names of the frames/panels you are using the methods on.

Community
  • 1
  • 1
Aarowaim
  • 801
  • 4
  • 10