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 4267874
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:57:31+00:00 2026-05-21T06:57:31+00:00

I just wrote this test code in my CustomUIPanel class: public static void main(String[]

  • 0

I just wrote this test code in my CustomUIPanel class:

public static void main(String[] args) {
    final JDialog dialog = CustomUIPanel.createDialog(null, 
       CustomUIPanel.selectFile());
    dialog.addWindowListener(new WindowAdapter() {
        @Override public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });
}

It works correctly if CustomUIPanel.main() is the program’s entry point, but it makes me wonder something: what if another class called CustomUIPanel.main() for testing? Then my call to System.exit(0) is incorrect.

Is there a way to tell the Swing event dispatch thread to exit automatically if there are no top-level windows?

If not, what’s the right thing for a JDialog/JFrame to do upon closing if the goal is for the program to exit when all the top level windows are closed?

  • 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-05-21T06:57:32+00:00Added an answer on May 21, 2026 at 6:57 am

    You can use the setDefaultCloseOperation() method of JDialog, specifying DISPOSE_ON_CLOSE:

    setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    

    See also 12.8 Program Exit.

    Addendum: Incorporating @camickr’s helpful answer, this example exits when either the window is closed or the close button is pressed.

    import java.awt.EventQueue;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.WindowEvent;
    import javax.swing.AbstractAction;
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JLabel;
    
    /** @see http://stackoverflow.com/questions/5540354 */
    public class DialogClose extends JDialog {
    
        public DialogClose() {
            this.setLayout(new GridLayout(0, 1));
            this.add(new JLabel("Dialog close test.", JLabel.CENTER));
            this.add(new JButton(new AbstractAction("Close") {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    DialogClose.this.setVisible(false);
                    DialogClose.this.dispatchEvent(new WindowEvent(
                        DialogClose.this, WindowEvent.WINDOW_CLOSING));
                }
            }));
        }
    
        private void display() {
            this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            this.pack();
            this.setLocationRelativeTo(null);
            this.setVisible(true);
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new DialogClose().display();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm running some test to prove a concept and i just wrote this code
I wrote a small queue class in C++, on Windows. Just to test, I'm
I just wrote a new web part and now I am getting this error
This code does not seem to compile, I just need to write something to
I just lost 50% of my answer on a test because I wrote the
I just wrote the following line of code: if (++(data_ptr->count) > threshold) { /*...*/
I wrote a short test code in Java to upload a PDF file generated
i wrote some code in codeigniter index controller using model ' navigation ': $this->load->model('navigation');
I have just upgraded my server 2003 to serrver 2008. As I write this,
I just wrote my first web service so lets make the assumption that my

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.