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.
Related
I would like to exit a console application while a javax.swing.JFrame still exists.
I have a console program that displays a javax.swing.JFrame. Sometimes I want to redirect the standard output to a file.
java -cp path com.domain.package > output.log
One plausible JFrame config value is WindowConstants.DISPOSE_ON_CLOSE and it is my understanding that the JFrame becomes disposed of when it is clicked to close, but the console program blocks at the closing brace of main. I do not know what it is waiting for. At that point I have to Control-C to terminate main.
Another plausible JFrame config value is WindowConstants.EXIT_ON_CLOSE and it is my understanding that when the JFrame is clicked to close, not only does the JFrame close but it also causes main to terminate. Note that even if main has reached the closing brace it does not exit until the click that closes the JFrame. This behaviour is better than the previous case because there is no need to Control-C to terminate main.
I want the console program to terminate so that the output.log file is released even if the JFrame is still alive. I tried to do this by having the JFrame owned by a Runnable in a Thread. That did not work; the console program does not terminate until the JFrame is closed.
// main program...
static void main(String[] args)
{
PlotWorker worker = new PlotWorker(data);
Thread thread = new Thread(worker);
thread.start();
// do not use thread.join
// simply exit at the closing brace of main
}
// owner of the JFrame...
class PlotWorker implements Runnable
{
JFrame jFrame;
#Override
public void run()
{
jFrame = new JFrame();
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
Realistically I know I can look at the output.log file with a text editor (or just Linux cat the file from a second console) even if the standard output in the first console still has a "handle" on the output.log. I would prefer a clean solution that exits while the JFrame still exists.
EDIT: This question is no longer important to me. I decided to screen capture and save to disk the JFrame image, thereby obviating the need to keep the JFrame open, then proceed to close the JFrame, thereby closing all files.
EXIT_ON_CLOSE and DISPOSE_ON_CLOSE to my knowledge only govern the application behavior in regards to what happens with the JFrame, I.E. if it's set to exit_on_close, you get a System.exit(0) when the JFrame is closed.
In general, I think System.exit(status) is what you want when terminating your application, this should dispose of any object created by it, release file-locks and close any windows. Might be a little messy if you're using a stream to write to your logfile because that will be terminated too, possibly without calling it's .close() function.
This question already has answers here:
java thread.sleep puts swing ui to sleep too
(3 answers)
Swing - Thread.sleep() stop JTextField.setText() working [duplicate]
(3 answers)
Closed 4 years ago.
I'm working on a game project where I'm creating a GUI using JSwing. It looks something like this:
A lonely 'F'
I'm trying to create a 'typewriter' effect, and so have a Thread.sleep(100) in my custom PrintUtils method (let's call it printToGui). I've routed System.out to the TextArea in the center of the GUI, and the first time I run the program the 'typewriter' effect works great and the startup text I send through printToGui shows up as it should. However, whenever I click a button (which triggers more text getting sent through printToGui), the app freezes, and won't unfreeze until the length of time it would have taken to print with Thread.sleep() finishes, spitting out all the text at once.
What I've figured out is that the first time I start the app, Thread.sleep() is happening on the "main" thread, while every time I click a button it's happening on the AWT-EventQueue-0 thread. I assume this is the problem, but maybe it's not; how do I get future output to the GUI to happen on the "main" thread and not the new one? Thanks!
How can I make my application pause when I open custom JDialog and after the dialog is closed to make continue again.
Simply use:
setModal(true);
I usually call it from within the constructor of the JDialog.
See the Javadocs on setModal(boolean).
http://java.sun.com/javase/6/docs/api/java/awt/Dialog.html#setModal(boolean)
That will cause execution to block on the current thread until the dialog box closes.
Alternatively, you can use:
setModalityType(Dialog.DEFAULT_MODALITY_TYPE);
It is equivalent to setModal(true) and technically the correct way to do it.
See the constructor of JDialog. You can set the modality of this dialog. Setting modal=true will pause your application. You can also use the method setModal.
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);
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