java – 等待jdialog关闭

我有一个类FilePathDialog,它扩展了jdialog,并且该类是从某个类X调用的.这是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();
            }                
        }
    });

jdialog窗口关闭时,doWork永远不会被调用.我所要做的就是等待jdialog关闭,然后再继续进行方法.我也尝试过使用SwingWorker和Runnable,但这没有帮助.

解决方法

同样,关键是对话模式与否?

如果它是模态的,则不需要WindowListener,因为您将知道对话框已被处理,因为代码将在对话框的setVisible(true)调用之后立即恢复.即,这应该工作:

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

另一方面,如果它没有模式,则WindowListener应该可以工作,并且您可能在此处未显示代码中遇到另一个问题,并且您将要发布sscce以供我们分析,运行和修改.

编辑gpeche
请查看此SSCCE,它显示3种类型的关闭参数将触发窗口监听器:

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);
   }
}

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...