Wait for jdialog to close

I have a FilePathDialog class that extends JDialog and this class is 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 is never called when the JDialog window closes. All I'm trying to do is wait for the JDialog to complete before it starts in the method. I also tried using SwingWorker and Runnable, but that didn't help.

+6
source share
3 answers

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); } } 
+17
source

Javadoc for WindowListener.windowClosed() :

Called when the window is closed as a result of the Locate in window call.

And for JDialog.setDefaultCloseOperation() :

Sets the operation to be performed by default when the user initiates a close in this dialog box. Possible options:

  • DO_NOTHING_ON_CLOSE - do nothing - requires the program to process the operation in the windowClosing method of the registered WindowListener object.
  • HIDE_ON_CLOSE - automatically hides the dialog after calling any registered WindowListener objects
  • DISPOSE_ON_CLOSE - automatically hides and removes the dialog box after calling any registered WindowListener objects

The default value is set to HIDE_ON_CLOSE.

This means that you must call projectDialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); after creating an instance of FilePathDialog .

+3
source

answer your question

add WindowListener to JDialog ( JDialog.DO_NOTHING_ON_CLOSE ), to windowClosing , to try to run your code if it dispose() , then call dispose() or setVisible(false)

I disagree with your idea, another workaround

create only one JDialog (JDialog.DO_NOTHING_ON_CLOSE) using WindowListener and reuse this for another Action , the final action will always be setVisible(false) if your code JDialog and then remove the child from JDialog , your JDialog prepared for another job

+2
source

Source: https://habr.com/ru/post/899831/


All Articles