Why does my application still run after closing main window?

If I make a JFrame like this

public static void main(String[] args) {

     new JFrame().setVisible(true);

then after closing the window the appication doesn't stop (I need to kill it).

What is the proper way of showing application's main windows ?

I'd also like to know a reason of a proposed solution.

Thanks in advance.

Asked by: Robert925 | Posted: 23-01-2022

Answer 1

You should call the setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); in your JFrame.

Example code:

public static void main(String[] args) {
    Runnable guiCreator = new Runnable() {
        public void run() {
            JFrame fenster = new JFrame("Hallo Welt mit Swing");

Answered by: Emily689 | Posted: 24-02-2022

Answer 2

There's a difference between the application window and the application itself... The window runs in its own thread, and finishing main() will not end the application if other threads are still active. When closing the window you should also make sure to close the application, possibly by calling System.exit(0);

Yuval =8-)

Answered by: Roland845 | Posted: 24-02-2022

Answer 3

You must dispose the frame, invoking the dispose method in your window listener or using setDefaultCloseOperation. For the argument of the last one, you can use two options:


DISPOSE_ON_CLOSE only dispose the frame resources.

EXIT_ON_CLOSE disposes the frame resources and then invokes System.exit.

There is no real difference between the two unless you have non daemon threads. I prefer to use DISPOSE_ON_CLOSE because this way I'm able to notice if I forgot to terminate a thread, because the JVM will stop if there are no more threads running. That's also the reason closing a Frame without disposing will not terminate the application, since Swing creates a thread to handle events that is terminated only when dispose is invoked.

Answered by: Rafael709 | Posted: 24-02-2022

Answer 4

The correct way to do this (unless you're writing a very trivial single-window app, i.e. no other windows or threads etc..) is to catch the windowClosing() event, and then call the dispose(); method of the form.

If your program doesn't completely exit after this, it means you have other non-deamon threads running, and you must stop these as best you see fit depending on your program.

Calling System.exit() or setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); will force stop any other threads (and the whole program), meaning your code is less portable, and force-stopping threads is obviously dangerous (in a programming kind of way).

Answered by: Freddie570 | Posted: 24-02-2022

Answer 5

You can set a window listener to the frame so the program terminates after you close it.

frame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {

Answered by: Thomas497 | Posted: 24-02-2022

Similar questions

How to handle a closing application event in Java?

Having a console application, a server accepting several connections from clients, is it possible to have a listener or an event on a closing application? I want, in this event, tell all connected clients to gently disconnect before the application really closes itself. Any solution? Thank you!

java - How can I stop my application from force closing?

I am making a example app for GPS positioning on my Evo 4G. This is to eventually be integrated into a app for time card use. But, it force closes. I don't have any ideas why... Any help? package com.Rick.GPS; import android.app.Activity; import android.content.Intent; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.locatio...

swing - How can I stop the closing of a Java application

I have a Java Desktop application and I would like when the user selects Exit to get a pop-up window that asks him if he wants to proceed with closing the application or not. I know how to make the window come up and read the user's response but what I need to know is how I can stop the application from closing (something like System.close().cancel()). Is that possible?

java - Get the application closing event

How to get to the application closing event? I want to get application to save all unsaved operations or show the user a message "Are you sure about closing without saving all data?" something like this. If this will be helpful - the application is a SingleFrameAplication using Swing.

java - Array that doesnt gets clear from memory when closing application

I need to have an int array which holds several Integer values. This array should not be clear when the user terminates the application. What I am going to use this for: Check the last item of this array if the last item in this array is 6, add 7 to the next item in the array The array cannot be deleted after the user exits the application, I need this for ge...

java - Gdx exit closing entire application - how to close only the gdx frame?

I have a game editor, in which it is possible to launch the game being edited in a separate window (but within the same VM). However, when the game is closed, I would like to close its gdx window, without bringing down the whole application (that is, the editor). I am currently using the following code inside the JFrame that hosts the LwjglApplication: public void windowClosing(WindowEv...

java - closing jframes current window and not the entire application

i need to close my applications current window without switching off the entire application my code is as follows to set a window visible but it closes all the windows whatever are currently open when i click on the right upper corner red cross button private void lb_helpMouseClicked(java.awt.event.MouseEvent evt) { new Reports().setVisible(true); } ...

java - Is heap memory freed after closing application?

If i create objects in the heap and i don't delete, when i close application that memory is blocked for other applications or simply it is free?. In general it's the same for c++, java and so and for all S.O? i have knowledge about garbage collector, and delete from C++, the only question is if i don't free the memory i use in the heap if that memory is stuck (until reset the device) for others applications or not)...

batch file - How to stop cmd .bat from closing up -- java jar application

I have this batch file java -jar evac.jar which runs a java console application. It works, but when it's done it auto-closes, thus proving pointless to me. How would I stop it from closing after execution so that I could analyze my data

java - Application closing Android

I'm creating a small android project. I'm new to java and android. When trying to open another activity in android application after 5 seconds of initial launch, instead of opening activity mentioned in intent, my application is getting closed. My java code for opening another intent looks as follows for the activity to be opened. Thread timer = new Thread() { @Override public void run() { ...

windows - IE6 generated strange worksheet name when doing export from java application

I am encountering error like test(10)[1].csv file cannot be found at C:\Documents and Settings\Ron\Local Settings\Temporary Internet Files\Content.IE5\PQ0STUVW When trying to do export of CSV file using the following codes. Anyone have any idea what could be wrong? This issue does not occur in IE7 / Firefox and is only specific to IE6. response.setContentType("applicati...

c# - Embedding Flash Player in a C++ or Java application?

I would like to embed Flash Player directly inside a C++ or Java application. I found an article that describes how to do this for C#: http://www.adobe.com/devnet/flash/articles/stock_history03.html Unfortunately, I have no experience with C#, COM or ActiveX. I need someone to translate this code to C++, allowing ...

java - How best to implement user selectable variables in web application

I have a Java based web-application and a new requirement to allow Users to place variables into text fields that are replaced when a document or other output is produced. How have others gone about this? I was thinking of having a pre-defined set of variables such as : @BOOKING_NUMBER@ @INVOICE_NUMBER@ Then when a user enters some text they can specify a variable inline ...

java - BIRT in a desktop application

Did someone ever used a BIRT report in a desktop application. I'm comming from the .NET environment and there you can use Crystal Reports to show reports in desktop apps. Is this possible with BIRT too, without having to set up a server environment? Can you give me some advice how to reach this goal? Thanks in advance.

which library better for faster java application swt or swing?

which library better for faster java application swt or swing?

java - Which is the best Open source application server?

java - Access spring bean that is exposed using http invoker from GWT application

Can I access spring bean that exposed using http invoker (server) from GWT application (client)? If so is there any example / tutorial for that?

How to close a Java Swing application from the code

What is the proper way to terminate a Swing application from the code, and what are the pitfalls? I'd tried to close my application automatically after a timer fires. But just calling dispose() on the JFrame didn't do the trick - the window vanished but the application did not terminate. However when closing the window with the close button, the application does terminate. What should I do...

java - How best can I isolate my application from an unreliable database?

I have a Java SOAP data service which sits on top of a Sybase database which, for reasons out of my control, has unreliable performance. The database is part of a vendor package which has been modified by an internal team and most of the issues are caused by slow response times at certain times of the day. The SOAP service provides data to a calculation grid and when I request data, I need the response time to be ...

java - Netbeans GUI Designer & Fixed-Size Application Panels

I'm having a problem, creating a fixed-size overall panel for a touchscreen GUI application that has to take up the entire screen. In a nutshell, the touchscreen is 800 x 600 pixels, and therefore I want the main GUI panel to be that size. When I start a new GUI project in NetBeans, I set the properties of the main panel for min/max/preferred size to 800 x 600, and the panel within the 'Design' view changes size. ...

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)