5

Possible Duplicate:
How to programmatically close a JFrame

I am developing a java GUI using JFrame. I want to close the GUI frame and dispose it off through code. I have implemented :

topFrame.addWindowListener(new WindowListener()
        {
            public void windowClosing(WindowEvent e)
            {
                emsClient.close();
            }
            public void windowOpened(WindowEvent e) {
            }
            public void windowClosed(WindowEvent e) {
            }
            public void windowIconified(WindowEvent e) {
            }
            public void windowDeiconified(WindowEvent e) {
            }
            public void windowActivated(WindowEvent e) {
            }
            public void windowDeactivated(WindowEvent e) {
            }
        });`

How can I invoke the windowClosing event?? Or is there some other way?

Community
  • 1
  • 1
user673218
  • 411
  • 2
  • 8
  • 20
  • 1
    Please look in below thread for the same purpose: http://stackoverflow.com/questions/258099/how-to-close-a-java-swing-application-from-the-code – Phani Apr 29 '11 at 09:26

4 Answers4

18

This will programmatically trigger the window closing event:

topFrame.dispatchEvent(new WindowEvent(topFrame, WindowEvent.WINDOW_CLOSING));

If you want to close the frame you need to call:

topFrame.dispose();
WhiteFang34
  • 70,765
  • 18
  • 106
  • 111
3

How about invoking dispose() method?

Lukasz
  • 7,572
  • 4
  • 41
  • 50
2

You need this:

yourFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

You can add that line in the constructor(Dont forget it).

javing
  • 12,307
  • 35
  • 138
  • 211
  • 3
    Well, I think the OP wants to close it programmatically. – Lukasz Apr 29 '11 at 09:26
  • If so he just need to catch the event for closing and call dispose(). But i don't understand, why not just adding that method in the constructor. Some IDEs add it even automatically when creating a JFrame. – javing Apr 29 '11 at 09:38
2
import java.awt.event.*;
import javax.swing.*;

class CloseFrame {

    public static void main(String[] args) {

        Runnable r = new Runnable() {

            public void run() {
                JButton close = new JButton("Close me programmatically");
                final JFrame f = new JFrame("Close Me");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setContentPane( close );
                close.addActionListener( new ActionListener(){
                    public void actionPerformed(ActionEvent ae) {
                        // make the app. end (programatically)
                        f.dispose();
                    }
                } );
                f.pack();
                f.setLocationByPlatform(true);
                f.setVisible(true);
            }
        };

        SwingUtilities.invokeLater(r);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Excellent [sscce](http://sscce.org/), but it won't trigger the questioner's `WindowListener`; I'm guessing that was was the goal, based on the accepted answer. – trashgod Apr 29 '11 at 20:44
  • 1
    Just to go off-topic, thanks for adding the Mac. screen shot to the Nested Layout Example. :) – Andrew Thompson Apr 30 '11 at 04:08