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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:54:22+00:00 2026-05-26T08:54:22+00:00

I have a class FilePathDialog which extends JDialog and that class is being called

  • 0

I have a class FilePathDialog which extends JDialog and that class is being called from some class X. Here is a method in class X

    projectDialog = new FilePathDialog();   
    projectDialog.setVisible(true);

    projectDialog.addWindowListener(new WindowAdapter() {            
        public void windowClosing(WindowEvent e) {
            System.out.println("Window closing");
            try {
                doWork();
            } catch (Throwable t) {
                t.printStackTrace();
            }                
        }

        public void windowClosed(WindowEvent e) {
            System.out.println("Window closed");
            try {
                doWork();
            } catch (Throwable t) {
                t.printStackTrace();
            }                
        }
    });     

doWork never gets called when the JDialog window closes. All I’m trying to do is wait for the JDialog to close before it proceeds in the method. I also tried using SwingWorker and Runnable but that did not help.

  • 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-26T08:54:23+00:00Added an answer on May 26, 2026 at 8:54 am

    Again, the key is is the dialog modal or not?

    If it’s modal, then there’s no need for a WindowListener as you will know that the dialog has been dealt with since code will resume immediately below your call to setVisible(true) on the dialog. i.e., this should work:

    projectDialog = new FilePathDialog();   
    projectDialog.setVisible(true);
    doWork(); // will not be called until the dialog is no longer visible
    

    If on the other hand it’s mode-less, then a WindowListener should work, and you’ve likely got another problem in code not shown here, and you’ll want to post an sscce for us to analyze, run, and modify.

    Edit for gpeche
    Please check out this SSCCE that shows that the 3 types of default closing parameters will trigger the window listener:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class DialogClosing {
       private static void createAndShowGui() {
          JFrame frame = new JFrame("DialogClosing");
    
          JPanel mainPanel = new JPanel();
          mainPanel.add(new JButton(new MyAction(frame, JDialog.DISPOSE_ON_CLOSE, "DISPOSE_ON_CLOSE")));
          mainPanel.add(new JButton(new MyAction(frame, JDialog.HIDE_ON_CLOSE, "HIDE_ON_CLOSE")));
          mainPanel.add(new JButton(new MyAction(frame, JDialog.DO_NOTHING_ON_CLOSE, "DO_NOTHING_ON_CLOSE")));
    
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
    class MyAction extends AbstractAction {
       private JDialog dialog;
       private String title;
    
       public MyAction(JFrame frame, int defaultCloseOp, final String title) {
          super(title);
          dialog = new JDialog(frame, title, false);
          dialog.setDefaultCloseOperation(defaultCloseOp);
          dialog.setPreferredSize(new Dimension(300, 200));
          dialog.pack();
          dialog.addWindowListener(new WindowAdapter() {
             @Override
             public void windowClosed(WindowEvent e) {
                System.out.println(title + " window closed");
             }
             @Override
             public void windowClosing(WindowEvent e) {
                System.out.println(title + " window closing");
             }
          });
    
          this.title = title;
       }
    
       @Override
       public void actionPerformed(ActionEvent arg0) {
          dialog.setVisible(true);
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class MyForm which inherited from QMainWindow . Here's my code: std::auto_ptr<MyForm> pForm(new
I have class which has a method that needs to return three DataTables. I
I have class that extends BroadcastReceiver and gets all new sms. When I get
I have class method that returns a list of employees that I can iterate
I have class X and in it there is a static method called doStuff()
I have class A that has some primitive attributes and also member of type
I have class deriving from TextBox to which I attach a dependency property of
I have: class MyClass extends MyClass2 implements Serializable { //... } In MyClass2 is
I have class A: public class ClassA<T> Class B derives from A: public class
I have class with a member function that takes a default argument. struct Class

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.