tags:

views:

201

answers:

5

how do i close a frame yet open a new frame?

i have a frame, (help)

when i click on my menu item i want to open (mainForm) exit from help.

new mainForm().setVisible(true); System.exit(0);

i know this closes the whole program however how do i get it to only close the current frame

thanks

+1  A: 

I think you should hide the frame you do not wish shown with setVisible(false). System.exit(0) stops the JVM ending the entire program.

In short, the semantics of setVisible has nothing to do with starting / stopping the application.

If you want to start an entire new application you'd have to look at Runtime.exec(). I don't really know if you can close the parent process (the Help application) with exit() though.

extraneon
i think i may have confused you.here is the scenario.i am in help i click on the menu bar where it says main. when main is clicked i want it to show main and hide help.if i set that to false it will also hide main
Tuffy G
there is no exit() method for a Frame. 'exit' is for the entire system
TStamper
I know. I was wondering whether you can start a new process from within the JVM, and then close the current process while not killing the newly started process also.
extraneon
yes you can do that if their both independently created processes
TStamper
+2  A: 

If you no longer want to use the frame you could use frame.dispose()

If you just want to hide it use frame.setVisible(false).

If you extended a Frame and are trying to close it from within use this.dispose or this.setVisible(false).

Gordon
+1  A: 

Let's say you created your frame as so:

 JFrame mainframe = new JFrame("Radio Notes");
//show Frame
mainframe.setVisible(true);
//close the frame  
mainframe.dispose();
TStamper
it's not main frame iam trying to get rid of, its help. when i do help.dispose(); it doesn't work
Tuffy G
mainframe was just an example of a created frame, if you named the frame help then yes help.dispose will close it; for example if you created a frame: JFrame help= new JFrame("help"); then help.dispose() will close it
TStamper
+2  A: 

You should rethink your requirments. For the user, it would be best to have both the program and the help window visible at the same time. Closing the main window when showing the help screen and vice versa is really, really bad for usability - you shouldn't do it. We've had window-based GUIs for almost 30 years now - showing several windows on screen at the same time is what they're for!

Michael Borgwardt
thanks. i'll take your advice
Tuffy G
A: 

try setting the default close operation for the JFrame like so.

frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

Then implement a WindowListener that performs the actions you want when a window closing event is fired.

ChadNC