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.
Related
Dear all please help me
when I run the project it stills running all over the time
when I repeat running multi-times I found processor processes more then 10 project s !!!!!
NetBeans allows multiple executions of the same project at the same time. What's probably happening is that app is not shutting down when you close its main window.
The reason: closing a JFrame window doesn't necessarily shut down your application. Unless you say otherwise, the default behavior is to simply hide the window and keep the application running.
If you check the DefaultCloseOperation property of your app's JFrame, you'll probably find that it's currently set to HIDE_ON_CLOSE. Change that to EXIT_ON_CLOSE and your application will shut down when the window is closed.
Note that you can still start multiple copies of your application at the same time, if you so desire. But with the application actually shutting down when you close its window, you're now less likely to find yourself accidentally knee-deep in multiple running copies that you thought you had previously killed.
Read all about window-closing options here.
I am writing an app that displays some information in a window when incoming call arrives. To do it I have a broadcast receiver which starts the service, then the service gets needed information and opens the window using windowmanager object. I must keep the service running as long as the user doesn't close the window to prevent window closing unexpectedly. The problem is: the window gets closed at unexpected time because the system kills the service. I need that window to stay opened until user closes it. I need your suggestion what can I do to make that window stay opened as long as the user doesn't close it. And if there's a way without keeping the service running it would be awesome too. Maybe I should start a non full screen activity instead of window? Then the system wouldn't kill it so aggressively. Please suggest me something. Thanks in advance!
Hello I have a little big problem in JavaFX application with terminating java process when all brower windows are closed. I was trying to handle Stage.setOnHiding or .setOnCloseRequest and terminate all running threads and do Platform.exit in the handler body, but with no luck. First of all none of handlers setOnHiding and setOnCloseRequest are invoked when I'm closing browser. Also setting Platform.setImplicitExit(true) does not cause java process to terminate when browser is closed - this works only sometimes. Sth prevents javafx process from being killed after web browser is closed. How can I detect what ?
For me, JavaFX applications hosted in a browser always terminate automatically when the browser is closed.
You should implement Application.stop() to detect the application shutdown event.
Stage level methods setOnCloseRequest and setOnHiding are not the right methods for detecting an application level shutdown event.
Apple JVM on various OS X version have apparently been broken in that they do not generate the WindowClosing event when they should (for example if you close an app's main JFrame using by clicking on the close button).
(in the most recent Apple Java updates you can set a property forcing the event to be generated but this is not what I'm looking for)
My problem is simple: I'd like to display a "tip" when the user closes the app. However I cannot (due to the fact that no event is generated) detected that the user closed the window.
So I thought I could use a shutdown hook:
Runtime.getRuntime().addShutdownHook(...)
However apparently creating a JFrame from a shutdown hook seems problematic: it's like if the EDT was already gone once the shutdown hook is called.
I tried several things and nothing really seems to makes sense: like my "Tip" JFrame staying all gray (despite it working fine when called from anywhere but the shutdown hook) or the program exiting immediately. I tried using a latch and waiting on the latch from the shutdown hook but it's as if the EDT wasn't there anymore.
I'm currently seriously considering spawning a second Java app just to display the tooltip as a workaround but I think it's a bit overkill (but at least it would work).
Did anyone ever try to create a window from a shutdown hook and invoke things on the EDT and are there any gotchas to be aware of? (remember that I cannot reliably catch the window closing events on OS X due to known very-longstanding Apple VM bugs).
If the window is actually closing and the application is stopping then something is calling the JFrame.dispose() method. overwrite this, and add your code there.
otherwise you can add a daemon thread that listens to the closed method on the window listener, the daemon can add the tooltip and then dispose of the window. you can delay the dispose until the tooltip is done.
I've never heard of this bug, but things can only get better now that apple isnt releasing its own jdk's.
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()