title: Why does setSelected on JCheckBox lose effect?
body: <p>Can someone explain to me why I lost the selection (set by <code>setSelected()</code>) for <code>JCheckBox</code> when I put the <code>JOptionPane</code> into the <code>ItemListener</code>?
  Is this a bug ?</p>
  
  <p>It is curious, that if this process is delayed with <code>invokeLater()</code>, <code>setSelected()</code> works correctly as I expected.</p>
  
  <p><img src="https://i.stack.imgur.com/gGXHj.jpg" alt="enter image description here"> <img src="https://i.stack.imgur.com/FoFba.jpg" alt="enter image description here"></p>
  
  <p>from SSCCE</p>
  
  <pre><code>import javax.swing.*;
  import java.awt.*;
  import java.awt.event.*;
  
  public class ComponentEventDemo extends JPanel
          implements ComponentListener, ItemListener {
  
      private static final long serialVersionUID = 1L;
      private JFrame frame;
      private JTextArea display;
      private String newline = "\n";
      private JTextField field1;
  
      public ComponentEventDemo() {
          super(new BorderLayout());
          display = new JTextArea(10, 25);
          display.setEditable(false);
          JPanel panel = new JPanel(new GridLayout(0, 2));
          field1 = new JTextField();
          field1.setDisabledTextColor(Color.red);
          JCheckBox checkbox = new JCheckBox("Label visible", true);
          checkbox.addItemListener(this);
          panel.add(checkbox);
          panel.add(field1);
          panel.addComponentListener(this);
          JScrollPane scrollPane = new JScrollPane(display);
          frame = new JFrame("ComponentEventDemo");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.add(scrollPane, BorderLayout.CENTER);
          frame.add(panel, BorderLayout.SOUTH);
          frame.addComponentListener(this);
          frame.setLocation(200, 200);
          frame.pack();
          frame.setVisible(true);
      }
  
      public void itemStateChanged(ItemEvent evt) {
          System.out.println("STATE CHANGED!");
          if (evt.getStateChange() == ItemEvent.SELECTED) {
              //javax.swing.SwingUtilities.invokeLater(new Runnable() {
  
              //public void run() {
              int returnVal = JOptionPane.showConfirmDialog(display,
                      "Bla Bla Bla Text");
              if (returnVal == JOptionPane.OK_OPTION) {
                  field1.setText("SELECTED - OK btn");
              } else if (returnVal == JOptionPane.NO_OPTION) {
                  field1.setText("SELECTED - NO btn");
              } else if (returnVal == JOptionPane.CANCEL_OPTION) {
                  field1.setText("SELECTED - Cancel btn");
              } else if (returnVal == JOptionPane.CLOSED_OPTION) {
                  field1.setText("SELECTED - Close btn");
              }
              //}
              //});
          } else if (evt.getStateChange() == ItemEvent.DESELECTED) {
              field1.setText("DESELECTED");
          }
      }
  
      protected void displayMessage(String message) {
          display.append(message + newline);
          display.setCaretPosition(display.getDocument().getLength());
      }
  
      public void componentHidden(ComponentEvent e) {
          displayMessage(e.getComponent().getClass().getName() + " ---&gt; Hidden");
      }
  
      public void componentMoved(ComponentEvent e) {
          displayMessage(e.getComponent().getClass().getName() + " ---&gt; Moved");
      }
  
      public void componentResized(ComponentEvent e) {
          displayMessage(e.getComponent().getClass().getName() + " ---&gt; Resized ");
      }
  
      public void componentShown(ComponentEvent e) {
          displayMessage(e.getComponent().getClass().getName() + " ---&gt; Shown");
  
      }
  
      public static void main(String[] args) {
          javax.swing.SwingUtilities.invokeLater(new Runnable() {
  
              public void run() {
                  ComponentEventDemo ced = new ComponentEventDemo();
              }
          });
      }
  }
  </code></pre>