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

A JButton that is not being "showed" triggers an ActionListener

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version "1.7.0_25"
      Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
      Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Linux ******* 3.2.0-23-generic #36-Ubuntu SMP Tue Apr 10 20:39:51 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux


      A DESCRIPTION OF THE PROBLEM :
      Based on java.awt.Component#isShowing description I believe a JButton that returns false for this method should not call an ActionListener when user clicks on it. I have developed an simple demonstration to help understanding.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Using the demonstration provided, user must keep clicking (i.e. holding and releasing the mouse left button) as fast as possible ignoring the fact that the button will be removed. The only assertion available will fail.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The assertion should not fail.
      ACTUAL -
      The assertion fails.


      ERROR MESSAGES/STACK TRACES THAT OCCUR :

      Exception in thread "AWT-EventQueue-0" java.lang.AssertionError
      at TestButton$2.actionPerformed(TestButton.java:23)
      at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
      at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
      at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
      at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
      at javax.swing.plaf.basic.BasicButtonListener.focusLost(BasicButtonListener.java:198)
      at java.awt.Component.processFocusEvent(Component.java:6397)
      at java.awt.Component.processEvent(Component.java:6261)
      at java.awt.Container.processEvent(Container.java:2229)
      at java.awt.Component.dispatchEventImpl(Component.java:4861)
      at java.awt.Container.dispatchEventImpl(Container.java:2287)
      at java.awt.Component.dispatchEvent(Component.java:4687)
      at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1895)
      at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:947)
      at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:621)
      at java.awt.Component.dispatchEventImpl(Component.java:4731)
      at java.awt.Container.dispatchEventImpl(Container.java:2287)
      at java.awt.Component.dispatchEvent(Component.java:4687)
      at sun.awt.SunToolkit$1.run(SunToolkit.java:507)
      at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
      at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
      at java.awt.EventQueue.access$200(EventQueue.java:103)
      at java.awt.EventQueue$3.run(EventQueue.java:694)
      at java.awt.EventQueue$3.run(EventQueue.java:692)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
      at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
      at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
      at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.Dimension;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;

      import javax.swing.JButton;
      import javax.swing.JFrame;
      import javax.swing.JRootPane;
      import javax.swing.SwingUtilities;
      import javax.swing.WindowConstants;

      public class TestButton {

      private JFrame frame = new JFrame();

      private JRootPane createPane() {
      JRootPane pane = new JRootPane();
      final JButton button = new JButton("Keep clicking here!!!");
      pane.getContentPane().add(button);

      button.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
      assert button.isShowing();
      }
      });

      return pane;
      }

      public void initGUI() {
      this.frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

      this.frame.setMinimumSize(new Dimension(300, 100));
      this.frame.pack();
      this.frame.setVisible(true);

      new Thread(this.r).start();
      }

      Runnable r = new Runnable() {
      @Override
      public void run() {
      while (true) {
      try {
      Thread.sleep(2500L);
      } catch (InterruptedException e) {
      e.printStackTrace();
      }

      SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
      if (TestButton.this.frame.getContentPane().getComponentCount() > 0) {
      TestButton.this.frame.getContentPane().removeAll();
      } else {
      TestButton.this.frame.getContentPane().add(TestButton.this.createPane());
      }
      TestButton.this.frame.revalidate();
      TestButton.this.frame.repaint();
      }
      });

      }
      }
      };

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

      ---------- END SOURCE ----------

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: