How to stop a jframe from closing when the program stops? - java

I am creating a window with a jframe. But when the program terminates the window closes. I want to the window to stay up even when the program closes.
The window doesn't have any functionality apart from displaying from text. I have tried to edit the default close process with:
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
But this doesn't stop the window from closes when the program stops.
Is there a way to keep this window/jframe open even when the program stops?

Related

Why is it recommended to use setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)?

Why is it recommended to use setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); if there are ways to explicitly close the frame?
Calling setDefaultCloseOperation(EXIT_ON_CLOSE) causes the application to exit when the application receives a close window event from the operating system. Pressing the close (X) button on your window causes the operating system to generate a close window event and send it to your Java application. The close window event is processed by the AWT event loop in your Java application which will exit the application in response to the event. If you do not call this method the AWT event loop may not exit the application in response to the close window event but leave it running in the background.
JFrame.EXIT_ON_CLOSE stops the application from running in background. For example, if you didn't use JFrame.EXIT_ON_CLOSE, if your application has an active database connection, it will stay connected. To check this, you can open task manager and see the jar file still running even if its window is no longer visible.

Is it possible to detect when the console is closed in Java?

I am writing a program that will be saved as a .JAR and run from the console. I want the program to save to a file whenever a user closes its window, but I don't know if you can detect the window being closed. I know I could use a WindowListener in JFrame to detect the Close Event, run the save function, then end the program, but with what the program is and how I set it up I don't really want to do that. I am running on Windows 8, if that makes a difference.

Java applet destroy() with showConfirmDialog

I'm doing some bug fixing on an old applet, but I'm running into issues with applets destroy() being called while there is a confirm dialog open (JOptionPane.showConfirmDialog). Basically, If there is a dialog box open, and the browser window is closed, the dialog box stays open, and the applets keep running in the background (not in a browser window). Is there a way to close all the dialog boxes and ensure that all applets are destroyed when the browser window is closed?
EDIT: ok, to clarify, this confirm dialog is being opened in a thread that the applet starts. However, it has the instance of the applet that started it, and the confirm dialog is being started with JOptionPane.showConfirmDialog(applet_instance, ...) This should mean that the user is unable to access the browser while the dialog is up. However, I can still close the window behind the dialog, and this kills the applet, but not the thread that opened the dialog. How would I go about killing all the threads when the window is closed, or how do I actually stop the user from closing the window without acknowledging the dialog first?
EDIT 2: I just tried to make the dialog in the applet itself, at the end of the start() method. This results in a dialog right when the applet loads, but I can still close the browser window behind the dialog and the dialog remains...

Close application from task-bar when a modal dialog is opened

When a modal dialog is opened, is there any way to allow user close a JFrame by clicking Close on task-bar icon? I see that even if the dialog is orphan or a child of another frame, it still blocks the Close action.
It depends on the native OS you're running on, I don't know of any that do allow you to.
Here is a MSDN listing on it :
http://msdn.microsoft.com/en-us/library/aa969773.aspx
A modal dialog box is displayed by a function when the function needs
additional data from a user to continue. Because the function depends
on the modal dialog box to gather data, the modal dialog box also
prevents a user from activating other windows in the application while
it remains open.

How can I stop my application?

I have the main thread from which I start a window using invokeLater. I run my application from command line. So, when application is running I see the window and my command line is "blocked" by the application.
I can stop the application either by closing the window (as a result the command line is unblocked) or by typing Ctrl-C in the command line (as a result the window disappear).
I wanted to be able to stop the application by clicking on a button in the window of the application. I used setVisible(false) for that. But in this way I can achieve the goal only partially. My window really disappear but the command line is still blocked. So, the software is still running.
Well, I assume it's because some other threads are still running. But how can I easily close all these threads (like I do by closing the window of the application manually).
System.exit(0);
If it's a JFrame you're showing, you can tell it to exit the app when the frame is closed - the default is to just hide the frame:
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
This will exit the app if the user closes the window (top right [x] button often) , in addition you could have a Quit button whose event handler closes the window using myFrame.dispose();
You must finish all threads in order to stop your application. Just hiding the GUI will not finish the AWT-Thread. Have a look at the API of the GUI classes you use like the dispose-methods.
Try Window.dispose()

Categories

Resources