Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a method when closing the program?

Tags:

java

swing

I need to execute a method, (a method which creates a file), when I exit my program, how would I do this?

like image 749
Stan Avatar asked Apr 28 '11 19:04

Stan


People also ask

Which method is used to terminate a running program?

We used functions like exit() in C++ to abnormally terminate the execution of program, which function can we use in Java.

How do you end a method in Java?

Exit a Java Method using Returnexit() method in java is the simplest way to terminate the program in abnormal conditions. There is one more way to exit the java program using return keyword. return keyword completes execution of the method when used and returns the value from the function.

How do I close a program in JFrame?

EXIT_ON_CLOSE (defined in JFrame) : Exit the application using the System exit method. Use this only in applications. might still be useful: You can use setVisible(false) on your JFrame if you want to display the same frame again. Otherwise call dispose() to remove all of the native screen resources.


2 Answers

Add shutdown hook. See this javadoc.

Example:

public static void main(String[] args) {
    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
        public void run() {
            System.out.println("In shutdown hook");
        }
    }, "Shutdown-thread"));
}
like image 68
Victor Sorokin Avatar answered Oct 20 '22 00:10

Victor Sorokin


Since you are using Swing. When you close your application (by pressing the close button), you could simply hide your frame. Run the method you would want which creates the file and then exit the Frame. This would result in a graceful exit. Should there be any errors/exceptions, you can log that into a separate file.

Here is the code

package test;

import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.JFrame;

public class TestFrame extends JFrame{

    public TestFrame thisFrame;

    public TestFrame(){
        this.setSize(400, 400);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    }

    public static void main(String[] args){
        TestFrame test = new TestFrame();
        test.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentHidden(ComponentEvent e) {
                System.out.println("Replace sysout with your method call");
                ((JFrame)(e.getComponent())).dispose();
            }
        });
    }

}

Please be aware of using shutdown hooks. As given in the Javadoc, it states that

When the virtual machine is terminated due to user logoff or system shutdown the underlying operating system may only allow a fixed amount of time in which to shut down and exit. It is therefore inadvisable to attempt any user interaction or to perform a long-running computation in a shutdown hook

like image 43
bragboy Avatar answered Oct 20 '22 00:10

bragboy