I have a single frame created using Netbeans GUI builder when I view the frame properties one of the first options is default close operation the options listed are: DISPOSE_ON_CLOSE, HIDE_ON_CLOSE, DO_NOTHING_ON_CLOSE & EXIT_ON_CLOSE I understand the middle two but, whats the difference between DISPOSE_ON_CLOSE and EXIT_ON_CLOSE ? I have tried testing both but to me they do the same thing to me
EXIT_ON_CLOSE will terminate the program.
DISPOSE_ON_CLOSE will call dispose() on the frame, which will make it disappear and remove the resources it is using. You cannot bring it back, unlike hiding it.
See aslo JFrame.dispose() vs System.exit()
If you have a few JFrames open and you close the one that is set to EXIT_ON_CLOSEthen all of the frames will be closed.
The opposite applies to the one with the DISPOSE_ON_CLOSE i.e. only it will be closed
DISPOSE_ON_CLOSE - Disposes the window when it's closed. You cannot redisplay the window, although the object window is still available in the memory
Related
This question already has answers here:
Difference between dispose and exit on close in java
(3 answers)
Closed 8 years ago.
what is different between Dispose_On_Close and Exit_On_Close in setDefaultCloseOperation method in JFrame class?
As their name implies, dispose on close disposes the window when it's closed while exit on close exits the JVM on window close. If the window is a JFrame and if it is the last window showing and if there are no non-daemon threads running, they'll both do the same thing -- exit the jvm. Per the API:
When the last displayable window within the Java virtual machine (VM) is disposed of, the VM may terminate.
I generally prefer to use dispose on close because of this.
EXIT_ON_CLOSE ends the complete thread by calling System.exit(0). DISPOSE_ON_CLOSE only closes the jframe, the thread runs on until it's terminated by something else. You can test this with a program that has 2 JFrames with the 2 different close operations. If you close the one with EXIT_ON_CLOSE both Frames will close, but if you close the one with DISPOSE_ON_CLOSE set as close operation, only this one will disappear and the other one will stay.
I'm using a custom JFrame to implement a simple dialog in a Java application I'm working on.
After the user pushes the 'Apply' button in the window, it should close.
What would be the most conventional way to do this? Is setVisible(false) inside the class the best way? Is there any way more recommended?
To close a window in Swing like JFrame or JDialog you have two options.
Call dispose()
Just call dispose() method:
public void dispose()
Releases all of the native screen resources used by this Window, its
subcomponents, and all of its owned children. That is, the resources
for these Components will be destroyed, any memory they consume will
be returned to the OS, and they will be marked as undisplayable.
For instance:
frame.dispose();
dialog.dispose();
Dispatch a WINDOW_CLOSING event
You can dispatch a new WindowEvent like this:
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING));
If the window has attached a WindowListener it will be notified. If the frame's (or dialog's) default close operation is set then this action will be performed. The possible close operations are:
DO_NOTHING_ON_CLOSE (defined in WindowConstants): Don't do anything;
require the program to handle the operation in the windowClosing
method of a registered WindowListener object.
HIDE_ON_CLOSE (defined in WindowConstants): Automatically hide the
frame after invoking any registered WindowListener objects.
DISPOSE_ON_CLOSE (defined in WindowConstants): Automatically hide and
dispose the frame after invoking any registered WindowListener
objects.
EXIT_ON_CLOSE (defined in JFrame and not available for JDialog): Exit the application using the System exit method. Use this only in applications.
It depends on what you're trying to achieve. Setting the window to not visible will simply hide it but it will still be running in the background (JFrame/InternalFrame). You can use JDialog (See JOptionPane as an example) to create temporary frames which are truly closed when clicking on one of the buttons. You can also retrieve the selected option when the user closed the window (here : Javadoc). You can also forcibly close a window by firing an event to close it, like so:
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(
new WindowEvent(frame, WindowEvent.WINDOW_CLOSING)
)
Inside an actionlistener for example.
There are multiple operations which can be performed when you close a JFrame.
Suppose you have a JFrame
JFrame frame = new JFrame();
This one exits the JVM when closed.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
This one just hides the JFrame.
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
This one disposes the JFrame.
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
When the last displayable window within the Java virtual machine (VM) is disposed of, the VM may terminate
And the default is do nothing.
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
On javadoc, the HIDE_ON_CLOSE default option says that
Automatically hide the frame after invoking any registered WindowListener objects.
Now what it means "HIDE"? the realtive object is destroyed or just hide and continue using resources?
On javadoc, the HIDE_ON_CLOSE default option says that
Automatically hide the frame after invoking any registered WindowListener objects.
Now what it means "HIDE"? the realtive object is destroyed or just hide and continue using resources?
HIDE_ON_CLOSE is the same as JFrame.setVisible(false),
then JFrame in only hiden, invisible, isn't destroyed somehow (the same for JFrame.dispose()), by JFrame.setVisible(true) is again visible on the sceen and without any changes
EDIT
#giozh wrote and if i want to destroy the jframe (without close the
entire application)?
by default there isn't any reason, because by default there no reason to create another JFrame, don't do that, use CardLayout (with JFrame.pack() if is neccessary to change JFrames size on the screen)
and all those Object stays and increasing JVM memory, never will be CG'ed, then there isn't siginficant difference for JVM memory between JFrame.HIDE_ON_CLOSE, JFrame.DISPOSE_ON_CLOSE or JFrame.setVisible(false)
(in the casse that you hate CardLayout) you can to remove all JComponents from JFrames ContentPane, then to add new JComponents, set LayoutManager and last code lines (after all changes to the already visible JFrame is done) would be JFrame.(re)validate();, JFrame.repaint(); and JFrame.pack();
hide it in background without destroying it, keep it in memory
HIDE_ON_CLOSE means that it will just disppear but will be running in the background and consuming resources ,though not visible on the screen.
But using EXIT_ON_CLOSE,instead,makes it disappear as well as kills the application (Use this if you want to really close the application)
Good day!
I'm developping a small Java Application with NetBeans IDE that extends JFrame. I implemented several ways to close the App, for example pressing Ctrl-Q and pressing the X of the JFrame.
But before the actual closing I'd like the program to execute some additional code for me, to save some objects the application needs to reuse the next time it starts up.
What is the best way to change the entire programs closing behavior?
I'm thinking about overriding/replacing the defaultCloseOperation, something like this:
setDefaultCloseOperation( myOwnCloseOperation );
Thanks for any help!
setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE )
Add a WindowListener or WindowAdapter - overriding either the windowClosing() or windowClosed() methods (I forget which) to catch the closing event.
Call the save functionality from the window method.
Call setVisible(false) or dispose() (as discussed in the comments) at the end.
Sorry for really simple question, but I wasnt able to find anything in the net, probably because I dont know the right terms to look for.
When you have a desktop application, there are many so called children windows: one for options, one for "about" and so on. How to make them in Java Swing (with NetBeans tools - optional)?
So far I just created another JFrame and on relevant event opened it the same way Main function launches, well, main JFrame. But there's something wrong with this method: when I close child window via x in the upper right corner, whole program terminates as if I was closing the main window. This is probably because NetBeans auto generated code for x and I can find and change it somewhere... but still I have a feeling that there must be a simpler proper way to add children JFrames (or JPanels or whatever is it for children windows) ;)
... But there's something wrong with this
method: when I close child window via
x in the upper right corner, whole
program terminates as if I was closing
the main window. ...
Well, I would say you are on the right path. Just make sure that you do the following for your child frames :
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Then closing the child windows won't shut down your entire application.
HTH ! ;-)
Do you mean a Dialog Window (see How to Make Dialogs) that you likely want to be modal to block user input to all other windows in the program (see An Overview of Dialogs)?
Go to the frame-design mode -> select your frame -> in the properties, the fist option. Select Dispose on close.
If you don't see the props: Ctrl + Shift + 7 or Window (in the frame menu) -> Properties
If you create frames, they aren't considered children windows. You can not set an owner for them as you could a JDialog. If you did want to create another JFrame, then you would have to set the close operation
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
instead of
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
The EXIT_ON_CLOSE operation will call
System.exit(0);
which will terminate the JVM. Just remember, that if your main JFrame is set to
DISPOSE_ON_CLOSE
then you must make sure that all your other frames have been properly disposed of, otherwise it won't exit until they have been.