Search code examples
javamultithreadinguser-interfacerunnable

about override run() method of GUI in main method


The issue is when I close my GUI windows I wanna run a last method ( for example printList() ) but I couldn't manage to do it. This is my main method

public static void main(String args[]) {

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            try {
                patientTest2 screen = new patientTest2();
                screen.setVisible(true);
                screen.setResizable(false);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(patientTest2.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    });

patientTest2 is my JFrame class. I assume that if I put printList() before } catch (FileNotFoundException ex) { it should work and finally print my list to a file but it doesn't. I will be glad if you can help me and explain why of course_?


Solution

  • You should add a listener that extends WindowAdapter to your frame, and override the method windowClosing(WindowEvent e). In this method, you will be able to call any methods you want to call before the window is closed.