United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6690791 Even more ClassCasetException with TrayIcon
JDK-6690791 : Even more ClassCasetException with TrayIcon

Details
Type:
Bug
Submit Date:
2008-04-18
Status:
Resolved
Updated Date:
2011-01-19
Project Name:
JDK
Resolved Date:
2008-05-15
Component:
client-libs
OS:
generic
Sub-Component:
javax.swing
CPU:
generic
Priority:
P3
Resolution:
Fixed
Affected Versions:
7
Fixed Versions:

Related Reports
Backport:
Relates:

Sub Tasks

Description
There is one more place when we unsafely cast MouseEvent.getSource() to Component
and get ClassCastException is the source of the event is TrayIcon
(TrayIcon doesn't extend awt.Component)

Test to reproduce is here:
https://swinghelper.dev.java.net/source/browse/swinghelper/src/java/org/jdesktop/swinghelper/tray/JXTrayIcon.java?view=markup

For some reason it is reproduced only on Linux machines

Run this demo and click the red icon in the tray area

You'll have -

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: JXTrayIcon1 cannot be cast to java.awt.Component
        at javax.swing.MenuSelectionManager.processMouseEvent(MenuSelectionManager.java:204)
        at javax.swing.plaf.basic.BasicPopupMenuUI$MouseGrabber.eventDispatched(BasicPopupMenuUI.java:817)
        at java.awt.Toolkit$SelectiveAWTEventListener.eventDispatched(Toolkit.java:2353)

                                    

Comments
EVALUATION

see MenuSelectionManager.processMouseEvent()

the exact line is: 
Component source = (Component)event.getSource();

we must use event.getComponent() instead and check it for null after that
                                     
2008-04-18
EVALUATION

I reproduced the bug on SuSe 10.3 and on Windows XP too.
                                     
2008-04-18
SUGGESTED FIX

MenuSelectionManager.processMouseEvent() should be modified.
There is the line: 
Component source = (Component)event.getSource();

We must use event.getComponent() instead and check it for null after that.

Udiffs:
src/share/classes/javax/swing/MenuSelectionManager.java
@@ -216,13 +216,13 @@
         MenuElement path[];
         Vector tmp;
         int selectionSize;
         p = event.getPoint();
 
-        Component source = (Component)event.getSource();
+        Component source = event.getComponent();
 
-        if (!source.isShowing()) {
+        if ((source != null) && !source.isShowing()) {
             // This can happen if a mouseReleased removes the
             // containing component -- bug 4146684
             return;
         }
 
@@ -234,11 +234,13 @@
             && ((modifiers & (InputEvent.BUTTON1_MASK |
                               InputEvent.BUTTON2_MASK | InputEvent.BUTTON3_MASK)) !=0 )) {
             return;
         }
 
-        SwingUtilities.convertPointToScreen(p,source);
+        if (source != null) {
+            SwingUtilities.convertPointToScreen(p, source);
+        }
 
         screenX = p.x;
         screenY = p.y;
 
         tmp = (Vector)selection.clone();
                                     
2008-04-29
WORK AROUND

There are no workarounds.
                                     
2008-04-30



Hardware and Software, Engineered to Work Together