Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

close java frame using code [duplicate]

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?

like image 292
user673218 Avatar asked Apr 29 '11 09:04

user673218


People also ask

How do I close a JFrame programmatically?

We can also close the window by clicking on the cross button X. While clicking the X button at the top right corner, the JFrame window programmatically is sent to the window closing event.

How do I close a JFrame without closing another?

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.


1 Answers

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();
like image 100
WhiteFang34 Avatar answered Sep 21 '22 08:09

WhiteFang34