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

[macos] Modal dialog is positioned behind the owner window

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      macOS High Sierra Version 10.13.6 (17G9016)
      java version "1.8.0_231"

      A DESCRIPTION OF THE PROBLEM :
      I am seeing an issue when a modal dialog is shown after another modal dialog is closed (both having the same owner).

      In that case, the owner window has a focused appearance and clicking on the owner window brings the owner window in front of the dialog.

      Please note that this behavior does not occur when the application is freshly started. It only occurs after another application has been activated after the application was started.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      - Run the test program
      - Activate another program (e.g. Finder)
      - Focus the Main Frame of the test application
      - Click on the "Click Me" button
      - Close the dialog titled "Modal Dialog 1"
      - Click on the Main Frame window
      - Observe that the dialog titled "Modal Dialog 2" gets behind the owner window
      - Observe that the main frame its title bar has a focused appearance

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The modal dialog "Modal Dialog 2" always remains in front of its owner window
      The owner window keeps its inactive title bar appearance
      ACTUAL -
      The modal dialog "Modal Dialog 2" gets behind the owner window
      The owner window gets an active title bar appearance

      ---------- BEGIN SOURCE ----------
      import java.awt.Component;
      import java.awt.FlowLayout;
      import java.awt.Window;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;

      import javax.swing.JButton;
      import javax.swing.JDialog;
      import javax.swing.JFrame;
      import javax.swing.JTextField;
      import javax.swing.SwingUtilities;

      public class FocusWindowTest {
      public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
      test();
      }
      });
      }

      private static void test() {
      JButton button = new JButton("Click Me");
      button.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent event) {

      Window owner = SwingUtilities.getWindowAncestor((Component) event.getSource());

      JDialog dialog1 = new JDialog(owner, "Modal Dialog 1");
      dialog1.setModal(true);
      dialog1.getContentPane().setLayout(new FlowLayout());
      dialog1.getContentPane().add(new JTextField(20));
      dialog1.setSize(300, 300);
      dialog1.setLocation(50, 50);
      dialog1.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
      dialog1.setVisible(true);

      JDialog dialog2 = new JDialog(owner, "Modal Dialog 2");
      dialog2.setModal(true);
      dialog2.getContentPane().setLayout(new FlowLayout());
      dialog2.getContentPane().add(new JTextField(20));
      dialog2.setSize(300, 300);
      dialog2.setLocation(70, 70);
      dialog2.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
      dialog2.setVisible(true);
      }
      });

      JFrame frame = new JFrame("Main Frame");
      frame.getContentPane().setLayout(new FlowLayout());
      frame.getContentPane().add(new JTextField(20));
      frame.getContentPane().add(button);
      frame.setSize(400, 400);
      frame.setLocation(0, 0);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      If the call to dialog2.setvisible(true) is done inside a SwingUtilities.invokeLater, the behavior is not observed.
      Note that with this workaround the code is no longer blocking and thus cannot be used without refactoring the application.

      FREQUENCY : always


            kabhishek Kumar Abhishek (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated: