Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8972003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:06:21+00:00 2026-06-15T18:06:21+00:00

In my Swing chat application I have a logout button which is used to

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:06:23+00:00Added an answer on June 15, 2026 at 6:06 pm
    • 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);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Swing application that connect directly to MySql Database for user authentication.
I have a thread running in my Swing chat application to listen indefinitely through
My Swing Application's GUI is built using Window Builder Pro GUI editor. Layouts used
I have a swing application in which I want to use a JCheckbox with
My Swing Java application works fine on Mac OS X, but its title in
In my Swing chat application i am having the send button, one text area,
My swing app has a main window which has sever buttons. This JFrame is
I have developed a basic Chat application in Java. It consists of a server
I made a little chat applet using multicast. It works fine between computers on
Not really that familiar with Swing, and am writing a client-server UDP chat application.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.