How can I stop my application? - java

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()

Related

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

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?

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.

Java program ends when logging out from x-session

When I run a Java program with a GUI (e.g. a simple JFrame) and then ending the X session (logout), my Java program is terminated. Java programs that don't use a GUI keep running.
There is nothing wrong with that, but I'd like to know how the program is terminated. I don't get messages about uncaught exceptions or other issues. The exit code is 1. If the prog was terminated by a signal, the exit code would by 128+signal (afaik).
Can someone shed some light on this issue. Can I somehow prevent program termination or control the exit code for that case?
[Edit]
I'm aware of the defaultCloseOperation. I'm using EXIT_ON_CLOSE. But this would lead to a call to System.exit(0).
I also added a WindowListener to monitor which of its functions is called when my program ends. When I finish the program by clicking on the window-X, the function windowClosing() is called. When I logout and finish the X-session so that my program is terminated, no WindowListener function is called. My program just exits with exit code 1.
When I run a Java program with a GUI (e.g. a simple JFrame)
I don't know how X-session works, but when using a JFrame you need to be aware of the frames
setDefaultCloseOperation(...);
method. When EXIT_ON_CLOSE is used, the JVM will be exited right away.
When DISPOSE_ON_CLOSE, the JVM will be exited if it is the last frame open and there are no other Threads running to prevent the JVM from closing.

How to set action for close button?

I've created a stand-alone java desktop application in Netbeans 6.9. I want to set the action for the close button of my application. I want to know how and where to set the code for the action of that close button. Can anyone please help me regarding this?
You have to register an ActionListener on your close button. In this listener you can define what do to.
How add ActionListener to JButton in Java Swing
I think answers for How to close a java swing application from the code will be helpful too
Right-click on the button then, > Events > Action > actionPerformed. NetBeans will generate the action listener for you:)
Edit: If you want a close listener, then read here.
Once you have the handler working, one convenient approach is to "set the default button by invoking the setDefaultButton() method on a top-level container's root pane." See the tutorial section How to Use JButton Features for details.
I dislike to wake up the zombies but, here is what I would do (this is considering that you are working in a Window based application, otherwise this is going to be useless!!!):
I would set a window listener for the close operation here is the way to do it.
Then I will delete manually the temp folders and files...
Obviously the window listener I'm talking about would go on the last window you (as a user) should close, this would not work either if you want it to happen when the last window is closed, but there is not an order specified to do that, some workarounds for that is making all your windows to share a flag/counter to indicate if it is time to delete the temp files/folders.
Again if your application is going to work underground sometimes (I mean that you can dispose all windows without shutting down the application), or is a daemon this won't work
If you just want to exit the application when the close button is hit, you can use
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
as explained in the Oracle tutorial

Hiding a windowed application launched by a java application?

I am working on a Java application which has to launch a different application. If I launch the second application using Runtime.getRuntime().exec(), it becomes the active process and its window comes before my application's window. What I really want to do is launch the process in "hidden" mode so that its taskbar entry does not appear and its window is initially invisible or behind my application window. Then my application would make it visible or move it to the front when it is good and ready. Is this possible or am I asking for too much?
This is for a demo. So I am not worried about security issues.
Edit: Daniel's answer has given me an idea. What if I use Powershell to invoke the application instead of CMD.EXE? Will that let me start the app without the window and then bring the window back? I will be using to launch java to launch PowerShell to launch app, but what the heck!
You don't say what this other application is, but I'm assuming that it's one that you have no control over (i.e. you can't give it a parameter option to start up in a minimized mode or similar.) Rather than hide the application you're launching, can you just use the toFront() method on your window after the other application has launched to bring your window in front of the other? And then minimize your window when you want to reveal the other one?
I'm the first to admit it's a bit of a bodged solution, but it might work depending on what you're after.
You cannot provide these parameters, BUT you can use the "start" command (try it in cmd), which supports these parameters. Eventually you have to call it with a cmd.exe shell, but this will work!

Categories

Resources