This question already has answers here:
How to make JFrame exist independently without parent?
(2 answers)
Closed 8 years ago.
I display two JFrame objects at the end of my program. But when I close JFrames, main() thread does not stop. It waits for me to click on stop button (the red rectangle in the picture). But I want main to stop when I close all the JFrames. Is that possible?
I'd suggest writing a window closing listener:
Be careful about cleaning up after yourself. You could just call System.exit(), but perhaps a better solution would do resource cleanup.
The stop button and window listener could call the same method to ensure consistency.
You can set the default close operation when you initialize your frame using this command:
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
According to the Javadocs, this will 'Exit the application using the System exit method' when you close the frame. I just tried this, and if you have two JFrames open, closing one will close all frames and stop the main() thread.
Related
In my program it opens a window if an action is happened. After I have filled out information in this window, and entered a button, the window dispose().
The window is picked up in a program outside my main program, but when I close this window, my main program stops. How can I prevent this from happening?
Thanks for your help!
You can set the defalaultCloseOperation property of the second frame to DO_NOTHING_ON_CLOSE or DISPOSE_ON_CLOSE
Don't even use two frames. Use a modal JDialog instead for a secondary frame. See more at How to Use Dialogs. Read more about Modality. And for a good read, see The Use of Multiple JFrames, Good/Bad Practice?
Forget about number 1. and go straight to 2.
If using JFrame or extending it you can use setDefaultCloseOperation() method like:
frame.setDefaultCloseOperation(HIDE_ON_CLOSE);
// or
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
The dispose command is from the AWT Bundle, and this may cause problems, as you are attempting to "close" a swing window with an AWT command.
You can close the window with this command:
windowName.setVisable(false);
windowName is the name of the object representing the window. If you are extending a class, and have no object, you can use this
More Information on the Dispose Method:
"In general java.awt.Window.dispose() is used on a GUI component that is
a subclass of Window, in order for that specific GUI Window object (and
its children) to properly release and destroy native UI resources (such
as the screen). Garbage collection (and the eventual exiting of the
entire VM) may happen as a result of dispose(), but is not directly
connected with the dispose() call itself." From: https://www.java.net/node/684412
windowName.setVisable(false);
doesn't seems to be a good choice. What if user want to exit the program?
check this question - How to hide a JFrame in system tray of taskbar
In my program it opens a window if an action is happened. After I have filled out information in this window, and entered a button, the window dispose().
The window is picked up in a program outside my main program, but when I close this window, my main program stops. How can I prevent this from happening?
Thanks for your help!
You can set the defalaultCloseOperation property of the second frame to DO_NOTHING_ON_CLOSE or DISPOSE_ON_CLOSE
Don't even use two frames. Use a modal JDialog instead for a secondary frame. See more at How to Use Dialogs. Read more about Modality. And for a good read, see The Use of Multiple JFrames, Good/Bad Practice?
Forget about number 1. and go straight to 2.
If using JFrame or extending it you can use setDefaultCloseOperation() method like:
frame.setDefaultCloseOperation(HIDE_ON_CLOSE);
// or
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
The dispose command is from the AWT Bundle, and this may cause problems, as you are attempting to "close" a swing window with an AWT command.
You can close the window with this command:
windowName.setVisable(false);
windowName is the name of the object representing the window. If you are extending a class, and have no object, you can use this
More Information on the Dispose Method:
"In general java.awt.Window.dispose() is used on a GUI component that is
a subclass of Window, in order for that specific GUI Window object (and
its children) to properly release and destroy native UI resources (such
as the screen). Garbage collection (and the eventual exiting of the
entire VM) may happen as a result of dispose(), but is not directly
connected with the dispose() call itself." From: https://www.java.net/node/684412
windowName.setVisable(false);
doesn't seems to be a good choice. What if user want to exit the program?
check this question - How to hide a JFrame in system tray of taskbar
Two + 1 Questions:
I've an stand alone application and created a runnable jar for it. Now, when I double-click it, a JFrame window opens and I need to log in. The problem is that I can open multiple JFrame windows if I double-click on it. What I need is once I have logged in, anytime I try to open it again, it should show me the currently logged in window. Or to put it this way, create a single instance of that JFrame window.
When closing the window, I need to do some operations before closing. I know that I can use a WindowListener but this does not work when I shutdown the JFrame (as in terminate the application). Is there a way to do any action before terminating manually?
EDIT : Another question:
I did a setExtendedState(JFrame.ICONIFIED); for minimizing the JFrame window when 'x'(close) is clicked. It minimizes but vanishes from the task bar. Is there a way to keep in the task bar like how the normal minimize works?
1)
Have your main method search for another instance of the program. If one is found, focus that window and have it close itself. You can read more about that in a similar question here: Question
2)
Use a shutdown hook:
Runtime.getRuntime().addShutdownHook(new Thread() {
#Override
public void run() {...} //your code to run when closing the program
}
I have one JFrame. That JFrame has a window listener for closing events which are intercepted and provide the user with options prior to closing the entire program.
Must I call dipose() on a JFrame or am I safe to call only System.exit(0)? Secondly, should I only call dipose() on a JFrame or should I call dipose() followed by System.exit(0)?
My question is must I call dipose() on my JFrame object or can I safely call System.ext(0)?
Why would you ask this? You should solve you compile problem first, and then decide what approach you want to use. Don't use System.exit() just because you don't know how to get rid of a compile error
am using a WindowAdapter as
There is no need for you to keep a variable in order to reference your frame. The best approach to access the frame is to get the frame from the WindowEvent:
JFrame frame = (JFrame)event.getSource();
To answer your original question I would use dispose(). It will eventually invoke System.exit() if it is the last frame open in your application.
Edit:
I want to give the user an option to do something productive before closing the program down
You can check out Closing an Appliction for more ideas on this topic.
I have a simple GUI program that creates a new window (which contains a JTable) at some point in time. If the user closes the JTable, the main application closes as well. How do I prevent this? Would it have something to do with how it handles the window closing, or should I give it it's own thread, or what?
Set one of these close operation for your JFrame: HIDE_ON_CLOSE or DISPOSE_ON_CLOSE.
You are using EXIT_ON_CLOSE.
Here is a link to the JavaDoc method you can use.