JDialog close/closing events

JDialog FAQ: How do I capture the JDialog close event?

Solution

To get the event when a JDialog is closed, you can add a window listener using addWindowListener to your JDialog, or add a component listener, using addComponentListener.

I think that adding a WindowListener to your JDialog is most often used. However, usually you don't want to implement the entire WindowListener interface; typically you just add a WindowAdapter, and implement the methods you're interested in, like this:

jdialog.addWindowListener(new WindowAdapter() 
{
  public void windowClosed(WindowEvent e)
  {
    System.out.println("jdialog window closed event received");
  }

  public void windowClosing(WindowEvent e)
  {
    System.out.println("jdialog window closing event received");
  }
});

In a test on a Mac OS X system, I just saw where the windowClosing event was received when I clicked the red "close" button on a Mac JDialog, but the windowClosed method never received its event.

JDialog close event with ComponentListener

If for some reason that approach doesn't give you the "JDialog close" event information you need, you can also add a ComponentListener to your JDialog, like this:

jdialog.addComponentListener(new ComponentListener() {
  public void componentHidden(ComponentEvent e)
  {
    System.out.println("dialog hidden");
  }

  public void componentMoved(ComponentEvent e)
  {
    System.out.println("dialog moved");
  }

  public void componentResized(ComponentEvent e)
  {
    System.out.println("dialog resized");
  }

  public void componentShown(ComponentEvent e)
  {
    System.out.println("dialog shown");
  }
});

Note that in this case I have implemented a ComponentListener instead of a ComponentAdapter because I wanted you to see all the different methods that are available, and when these events fire. Once you know what event you want to capture, you can implement your solution with less code using a ComponentAdapter.

A complete JDialog close example

Finally, if you want to test all of this on your own computer, here's the source code for a complete Java class that demonstrates both of these JDialog close event examples.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * Demonstrate how to handle JDialog close
 * events with a WindowAdapter and a ComponentListener.
 */
public class JDialogCloseExample
{
  static JFrame frame;
  static JDialog jdialog;

  public static void main(String[] args)
  {
    // schedule this for the event dispatch thread (edt)
    SwingUtilities.invokeLater(new Runnable()
    {
      public void run()
      {
        displayJFrame();
      }
    });
  }

  static void displayJFrame()
  {
    frame = new JFrame("Our JDialog Close Example");

    // setup our jdialog listener
    jdialog = new JDialog(frame, "Hello", true);

    // add a window listener
    jdialog.addWindowListener(new WindowAdapter()
    {
      public void windowClosed(WindowEvent e)
      {
        System.out.println("jdialog window closed");
      }

      public void windowClosing(WindowEvent e)
      {
        System.out.println("jdialog window closing");
      }
    });

    // add a component listener
    jdialog.addComponentListener(new ComponentListener()
    {
      public void componentHidden(ComponentEvent e)
      {
        System.out.println("dialog hidden");
      }

      public void componentMoved(ComponentEvent e)
      {
        System.out.println("dialog moved");
      }

      public void componentResized(ComponentEvent e)
      {
        System.out.println("dialog resized");
      }

      public void componentShown(ComponentEvent e)
      {
        System.out.println("dialog shown");
      }
    });

    // display our jdialog when the jbutton is pressed
    JButton showDialogButton = new JButton("Click Me");
    showDialogButton.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        jdialog.setLocationRelativeTo(frame);
        jdialog.setVisible(true);
      }
    });

    // put the button on the frame
    frame.getContentPane().setLayout(new FlowLayout());
    frame.add(showDialogButton);

    // set up the jframe, then display it
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setPreferredSize(new Dimension(300, 200));
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}

More JDialog close event information

For more information on the Java classes shown here, and handling the JDialog close events, check out these Javadoc pages: