Java – wait for jdialog to close

I have a class filepathdialog, which extends jdialog, and this class is called from a class X This 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 is never called when the jdialog window is closed All I have to do is wait for jdialog to close and then continue with the method I've also tried using swing worker and runnable, but it doesn't help

Solution

Similarly, the key is the dialogue mode or not?

If it is modal, windowlistener is not required because you will know that the dialog box has been processed because the code will be restored immediately after the setvisible (true) call of the dialog box That is, this should work:

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

On the other hand, if it has no schema, windowlistener should work, and you may encounter another problem in the code not shown here, and you will publish sscce for us to analyze, run and modify

To edit gpeche, view this sscce, which displays three types of default closing parameters that 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);
   }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>