JDK-6690791 : Even more ClassCasetException with TrayIcon
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 7
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2008-04-18
  • Updated: 2011-01-19
  • Resolved: 2008-05-15
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
6u10Fixed 7 b27Fixed
Related Reports
Relates :  
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:

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)

WORK AROUND There are no workarounds.

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();

EVALUATION I reproduced the bug on SuSe 10.3 and on Windows XP too.

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