A parentless JFileChooser after a JOptionPane prevents application exit

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      System irrelevant (tested on Windows 11, MacOS Sequoia 15.6.1).
      JDKs tested: Temurin-25.0.1 and Temurin-17.0.17.

      A DESCRIPTION OF THE PROBLEM :
      A parentless JFileChooser (does not matter if a file is chosen or not) after a parentless JOptionPane prevents application exit. I suspect this code in JOptionPane tries to prevent this, but it's missing in JFileChooser:
      ```java
      if (window instanceof SwingUtilities.SharedOwnerFrame) {
          WindowListener ownerShutdownListener =
                  SwingUtilities.getSharedOwnerFrameShutdownListener();
          dialog.addWindowListener(ownerShutdownListener);
      }
      ```

      In other words, if a `JFileChooser` has a `SwingUtilities.SharedOwnerFrame` instance for a parent, it does not properly `dispose()` it and the shared owner frame lives forever.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      See the attached code. Note that if the JOptionPane comes after the JFileChooser, the application exits correctly. The same happens when either one of the dialogs (or both) have an actual properly-behaved parent which eventually gets disposed and the application exits.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The application should exit.
      ACTUAL -
      There is a hanging, undisposed `SwingUtilities.SharedOwnerFrame` which prevents application exit.

      ---------- BEGIN SOURCE ----------
      public class Playground {

      public static void main(String... args) {
      SwingUtilities.invokeLater(() -> {
      JOptionPane.showMessageDialog(null, "This is a test, do not panic.");

      var result = new JFileChooser().showOpenDialog(null);
      IO.println(result);

      // ...hangs forever, unless:

      // This fixes it by manually disposing of the problematic frame:
      // JOptionPane.getRootFrame().dispose();

      // This is a cleaner version of the workaround:
      /*
      for (Window window : Window.getOwnerlessWindows()) {
      window.dispose();
      }
      */
      });
      }

      }
      ---------- END SOURCE ----------

            Assignee:
            Tejesh R
            Reporter:
            Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: