1

In my Swing chat application I have a logout button which is used to logout the user and it works fine. Now I need to logout the user when I close the Swing application window.

I did this in web application when closing browser using JavaScript, but now I need to do this in Swing application.

How can I achieve this?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Human Being
  • 8,269
  • 28
  • 93
  • 136

2 Answers2

6
  • Call JFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE)
  • Add a WindowListener to the frame.
  • Override the appropriate method of the listener to call your closing method, then set the frame invisible and dispose of it.

E.G.

Dialog, checking for frame exit

import java.awt.*;
import java.awt.event.*;
import java.net.URI;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

class CheckExit {

    public static void doSomething() {
        try {
            // do something irritating..
            URI uri = new URI(
                    "http://stackoverflow.com/users/418556/andrew-thompson");
            Desktop.getDesktop().browse(uri);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                // the GUI as seen by the user (without frame)
                JPanel gui = new JPanel(new BorderLayout());
                gui.setPreferredSize(new Dimension(400, 100));
                gui.setBackground(Color.WHITE);

                final JFrame f = new JFrame("Demo");
                f.setLocationByPlatform(true);
                f.add(gui);

                // Tell the frame to 'do nothing'.
                f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

                WindowListener listener = new WindowAdapter() {

                    @Override
                    public void windowClosing(WindowEvent we) {
                        int result = JOptionPane.showConfirmDialog(
                                f, "Close the application");
                        if (result==JOptionPane.OK_OPTION) {
                            doSomething();
                            f.setVisible(false);
                            f.dispose();
                        }
                    }
                };
                f.addWindowListener(listener);

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.pack();
                // should be done last, to avoid flickering, moving,
                // resizing artifacts.
                f.setVisible(true);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Thanks for your support...It worked but also need an confirmation box when I try to close the window... – Human Being Dec 10 '12 at 12:40
  • Thanks for your reply...and Worked fine but if I press Cancel button, My application got closed...I don't need to be close the application window ,if I press cancel button... – Human Being Dec 10 '12 at 13:22
1

Use the window events on your JFrame, there you have the Methods you might need (windowclosed();) for example. it´s the WindowListener

edit :

you can say

setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

but your Windowlistener still works if you push the X (close button)

there you override the method windowClosing, with this code

public void windowClosing(WindowEvent e) {
    int i = JOptionPane.showConfirmDialog(TestFrame.this, "do you really want to close?","test",JOptionPane.YES_NO_OPTION);
    if(i == 0) {
        System.exit(0);
    }
}

this will do the work

SomeJavaGuy
  • 7,307
  • 2
  • 21
  • 33
  • Thanks for your support...It worked but also need an confirmation box when I try to close the window... – Human Being Dec 10 '12 at 12:41
  • Thanks for your reply...But if I press cancel or No button I dont need to close the application application....Pls help me.. – Human Being Dec 10 '12 at 13:14
  • TestFrame is my JFrame class. you can change this with whatever your JFrame is called. and i is the reply of my confirm message. so if you click yes when the question appears, your Application will shut down, otherwhise it does nothing – SomeJavaGuy Dec 10 '12 at 13:53
  • write down `classname.this.` or simply `this.` – SomeJavaGuy Dec 10 '12 at 14:53