title: Java Modal dialog freezes whole application when whole application loses focus right before it is opened
body: <p>Once the steps below are taken, the whole application is frozen and the modal dialog cannot be closed.</p>
  
  <p>(This is related to <a href="https://stackoverflow.com/questions/48676282/java-modal-dialog-freezes-whole-application-when-using-jdk-8">another question</a> but this time we have a reproducible scenario)</p>
  
  <p>The steps:</p>
  
  <ul>
  <li>Open dropdown</li>
  <li>Select "Han-Ra" (the last value in the drop down)</li>
  </ul>
  
  <p>After this, trying to resize or close the modal dialog will not succeed. It reproduces 1 out of 3 times (might be easier to reproduce if you do the selection by arrow down but happens with mouse selection also)</p>
  
  <p>This happens on jdk <code>1.8</code> (tried <code>1.8.0_162</code> and <code>1.8.0_144</code>) and <code>jdk 10</code> (<code>10.0.1</code>) but not when using <code>1.7</code> (tried <code>1.7.0_80</code>)</p>
  
  <p>This is just the most obvious case we could find but it randomly (rarely) happens for most modal dialogs.
  Anyone else had this problem and found a workaround? We'll report it to Oracle but we'd be more interested in a workaround.</p>
  
  <pre><code>import javax.swing.*;
  import javax.swing.event.PopupMenuEvent;
  import javax.swing.event.PopupMenuListener;
  import java.awt.*;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.event.KeyEvent;
  
  public class FreezePleeze {
  
      public static final Object[] ALL_THE_SINGLE_LADIES = {"Rahan", "Crao", "Naouna", "Han-ra"};
  
      public static void main(String[] args) {
          new FreezePleeze();
      }
  
      public FreezePleeze() {
          SwingUtilities.invokeLater(new Runnable() {
              @Override
              public void run() {
                  JButton push_me = new JButton("Push me");
                  JFrame frame = new JFrame("Mmmmm");
                  JPanel containerPanel = new JPanel();
                  frame.add(containerPanel);
                  final JComboBox&lt;Object&gt; comboBox = new JComboBox&lt;&gt;(ALL_THE_SINGLE_LADIES);
                  containerPanel.add(comboBox);
                  frame.setSize(300, 300);
  
                  comboBox.addActionListener(new ActionListener() {
                      @Override
                      public void actionPerformed(ActionEvent e) {
                          JDialog jDialog = new JDialog((JFrame) null, true);
                          jDialog.add(push_me);
                          if (comboBox.getSelectedIndex() == ALL_THE_SINGLE_LADIES.length - 1) {
                              jDialog.setLocationRelativeTo(frame);
                              jDialog.setSize(300, 300);
                              jDialog.setVisible(true);
                          }
                      }
                  });
  
                  comboBox.addPopupMenuListener(new PopupMenuListener() {
                      @Override
                      public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
                      }
  
                      @Override
                      public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
                          try {
                              Robot robot = new Robot();
                              robot.keyPress(KeyEvent.VK_WINDOWS);
                              robot.keyRelease(KeyEvent.VK_WINDOWS);
                          } catch (AWTException e1) {
                              e1.printStackTrace();
                          }
                          push_me.setText("Finished counting");
                      }
  
                      @Override
                      public void popupMenuCanceled(PopupMenuEvent e) {
                      }
                  });
                  frame.setVisible(true);
              }
          });
      }
  }
  </code></pre>