java – 等待jdialog关闭

前端之家收集整理的这篇文章主要介绍了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);
   }
}
原文链接:https://www.f2er.com/java/128016.html

猜你在找的Java相关文章