Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8306001

Modal dialog can go behind another modal dialog

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P3 P3
    • tbd
    • 8, 11, 17, 20, 21
    • client-libs
    • Linux specific issue

      ADDITIONAL SYSTEM INFORMATION :
      Linux and Mac, but not Windows

      A DESCRIPTION OF THE PROBLEM :
      A modal dialog created from another modal dialog but without that dialog as the parent can be sent behind the invoking modal dialog and then no further interaction is possible with the application until the leaf dialog can be brought back to the front (which is not possible if it is completely obscured by the invoking dialog).

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Run with `JFrame` and `JDialog` in undecorated mode (`setDefaultLookAndFeelDecorated(true)`
      2. Open a normal `JFrame` window, call it "Main"
      3. From Main, open a modal dialog (e.g. via `JOptionPane.showMessageDialog`), setting its parent to Main. Call this window "Dialog"
      4. From Dialog, open a second modal dialog, but set it's parent to Main rather than Dialog. Call this "Nested"
      5. Use your mouse to click Dialog and see that it come to the front

      If Nested is visible, you can bring it back to the front and interact with it normally. However, if Nested is completely obscured by Dialog, you cannot do anything other than kill the application.

      On Linux (at least CentOS 7.9) this is always an issue. On Mac OS X 13.3.1, Nested will try to come to the front, but isn't always successful. On Windows 10, you cannot bring Dialog to the front and the system beeps at you when clicking it.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The leaf level modal dialog (Nested in this example) remains at the front.
      ACTUAL -
      The leaf level modal dialog (Nested in this example) goes behind the invoking dialog.

      ---------- BEGIN SOURCE ----------
      package dialog;

      import java.awt.BorderLayout;
      import java.awt.Component;
      import java.awt.HeadlessException;
      import java.awt.event.ActionEvent;
      import javax.swing.JButton;
      import javax.swing.JDialog;
      import javax.swing.JFrame;
      import javax.swing.JOptionPane;
      import javax.swing.SwingUtilities;

      public class ModalBehind extends JFrame {

          public static void main(String... args) {
              JFrame.setDefaultLookAndFeelDecorated(true);
              JDialog.setDefaultLookAndFeelDecorated(true);

              SwingUtilities.invokeLater(() -> new ModalBehind().setVisible(true));
          }

          public ModalBehind() throws HeadlessException {
              super("Main Frame");
              getContentPane().setLayout(new BorderLayout());

              JButton button = new JButton("Open Dialog");
              button.addActionListener(e -> showDialog());
              getContentPane().add(button, BorderLayout.PAGE_END);
              pack();
          }

          private void showDialog() {
              JButton button = new JButton("Open Nested Dialog");
              button.addActionListener(this::showNestedDialog);
              JOptionPane.showMessageDialog(this, button, "Top Level Dialog", JOptionPane.PLAIN_MESSAGE);
          }

          private void showNestedDialog(ActionEvent e) {
              Component parent = this;
              // Uncommenting this resolves the issue but is not necessary when LookAndFeelDecorated is false
              // parent = (Component) e.getSource();
              JOptionPane.showMessageDialog(parent,
                      "When LookAndFeelDecorated is true and parent is the main frame,"
                      + " clicking the top level dialog will make this go behind.",
                      "Nested Modal Dialog", JOptionPane.PLAIN_MESSAGE);
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Ensure the parent is set to the invoking window when showing a modal dialog

      FREQUENCY : always


            tr Tejesh R
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: