Reputation: 450
I am creating a Java Application using netBeans and Swing Gui Builder. I am trying to create an application that closes the current window and opens a new one when a selection is made from the view menu. What would be the best way to do this?
EDIT: I am trying to create a desktop application.
Upvotes: 0
Views: 14224
Reputation: 6054
If you only have 2 windows that you want to swap between, it may be easiest to just use JFrame.setVisible() to swap between the two.
frame1.setVisible(false); //hides it temporarily
frame2.setVisible(true); //shows it
This doesn't actually close frame1--it just hides it and pops frame 2 into visibility.
If you are writing a program with many potential windows and you want to actually "destroy" the window (thus freeing up the extra memory it takes up) you need to call JFrame.dispose();
frame1.dispose(); //closes the window--cannot be recovered
frame2.setVisible(true); //shows it
Upvotes: 1