Again, is the key a modal dialogue or not ?
If this is modal, then there is no need for a WindowListener, since you will find out that the dialog has been examined, since the code will resume immediately under your call to setVisible(true) in 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 does not have an operating mode, then the WindowListener should work, and you probably had another problem in the code that is not shown here, and you want to publish sscce so that we can analyze, run and modify.
Edit for gpeche
Check out this SSCCE, which shows that three types of default close options will call 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); } }
source share