Hi,

Please find attached a patch to fix mouseclick actions in the JComboBox.

Previously, if you clicked on the combo box button while the popup menu
was already open, it would close and immediately re-open the popup; this
was caused by two listeners both catching the mouseclick and both
toggling the menu (ie toggled off, then immediately toggled back on).
This patch disables one of the listeners while the popup menu is
visible.

Unfortunately, the fix isn't evident at the moment due to a new bug in
JComponent introduced by the VolatileImage patch, but has been tested
against pre-VolatileImage code.  Not sure how to write a mauve test,
since it must simulate a mouseclick on a specific component's
location...

Regards,
Francis

2006-06-12  Francis Kung  <[EMAIL PROTECTED]>

        * javax/swing/plaf/basic/BasicComboPopup.java:
        (setVisible): new method

Index: javax/swing/plaf/basic/BasicComboPopup.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicComboPopup.java,v
retrieving revision 1.18
diff -u -r1.18 BasicComboPopup.java
--- javax/swing/plaf/basic/BasicComboPopup.java	16 May 2006 18:57:33 -0000	1.18
+++ javax/swing/plaf/basic/BasicComboPopup.java	12 Jun 2006 17:37:57 -0000
@@ -222,6 +222,40 @@
       }
     comboBox.repaint();
   }
+  
+  /**
+   * Sets visibility property of this popup menu. If the property is set to true
+   * then popup menu will be dispayed and popup menu will hide itself if visible
+   * property is set to false.
+   * 
+   * @param visible true if popup menu will become visible and false otherwise.
+   */
+  public void setVisible(final boolean visible)
+  {
+    super.setVisible(visible);
+
+    // If the popup is visible and the user clicks anywhere outside of the popup
+    // menu, BasicLookAndFeel will close the popup for us (via
+    // MenuSelectionManager): so we disable our own listener when the popup
+    // becomes visible, and re-enable our listener when the popup is invisible.
+    //
+    // This must be invoked later in the event dispatcher thread, otherwise
+    // we still have the same lister-duplication problem due to the ordering
+    // of the dispatcher
+
+    SwingUtilities.invokeLater(new Runnable()
+    {
+      public void run()
+      {
+        if (visible)
+          ((BasicComboBoxUI) comboBox.getUI()).arrowButton.removeMouseListener(mouseListener);
+
+        else
+          ((BasicComboBoxUI) comboBox.getUI()).arrowButton.addMouseListener(mouseListener);
+
+      }
+    });
+  }
 
   /**
    * Return list cointaining JComboBox's items

Reply via email to