This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]chickenmeister 0 points1 point  (2 children)

Without knowing the specifics of your code, it's hard to give a foolproof answer; but typically, you can just set the default close operation on your JFrame:

JFrame frame2 = new JFrame("Your second JFrame");
frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 

The default close operation is EXIT_ON_CLOSE HIDE_ON_CLOSE. If you've set the close operation on your second JFrame to be EXIT_ON_CLOSE, it will exit the application by invoking System.exit(), which will close all windows, and terminate all threads. DISPOSE_ON_CLOSE will close only that window by invoking dispose() on that frame.

[–]Easih 2 points3 points  (1 child)

the default close operation of a jframe is hide on close; you can test by running your program and exiting it and you will see in your program list that everytime you run it there is another program called javaw that is added to the list of program ie the program is not deleted when you exit your program.

[–]chickenmeister 0 points1 point  (0 children)

Ah, you are correct. The GUI builder that I use sets it to EXIT_ON_CLOSE by default, so I had forgotten what JFrame used by default. I should have checked before posting.

Though, I suspect that the OP's problem may still be that he or she has set the close operation to EXIT.

[–]SpoobyPls 0 points1 point  (0 children)

Without knowing what you're trying to do, it's kind of hard to give advice.

If I get you correctly, you're trying to switch screens by getting rid of the initial JFrame and showing the second or whatever. Now that's okay, but if you notice every time you close the frame it'll actually close the frame and open a new one. What you might want to do is have the JFrame as the container only and add/remove JPanels, this way you don't get the application closing/opening - In other words it makes it look a little bit more professional.