Note: This Java deployment guide describes features released prior to the Java SE 6 update 10 release. See Java Rich Internet Applications Development and Deployment for the latest information.
java.lang.ClassCastException
Thrown From the AWT
Event Dispatching Thread When Mouse Moves Over an Applet FrameWhile running an applet in a browser by using the Sun
Java™ Runtime Environment (JRE™) implementation, a
java.lang.ClassCastException
is thrown from the
Abstract Window Toolkit (AWT) event-dispatching thread when the
mouse moves over the applet's frame, as shown in the following
code:
java.lang.ClassCastException: sun.plugin....
at
MyApplet.mouseExit(Unknown Source)
at
java.awt.Component.handleEvent(Unknown Source)
at
java.awt.Component.postEvent(Unknown Source)
at
java.awt.Component.dispatchEventImpl(Unknown Source)
at
java.awt.Container.dispatchEventImpl(Unknown Source)
at
java.awt.Component.dispatchEvent(Unknown Source)
at
java.awt.EventQueue.dispatchEvent(Unknown Source)
at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown
Source)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown
Source)
at
java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at
java.awt.EventDispatchThread.run(Unknown Source)
The applet runs without any error with the Microsoft Virtual Machine (VM).
This exception has two possible causes:
MouseListener
event on the frame. In the
Microsoft VM implementation, the applet's direct parent in the AWT
hierarchical component tree is the frame. So, some applets rely on
the following code:public void foo()
{
Frame f = (Frame)
getParent();
....
}
getParent()
method does not
return a Frame
object, and the above code results in
java.lang.ClassCastException
error.java.applet.AppletContext
interface is implemented by
the applet frame. So, some applets rely on the following
code:public void foo()
{
Frame f = (Frame)
getParent();
....
AppletContext ac =
(AppletContext) f;
....
}
But applet's direct parent is specific to the implementation and
might change. Because the Sun JRE implements
AppletContext
using a different object, the above code
results in a java.lang.ClassCastException
.
In the first cause, navigate the entire AWT hierarchical component tree from the applet to locate the frame, instead of relying on a frame at a particular level. The following code shows how you can navigate the entire component tree:
public void foo()
{
// Navigate component
tree
Container c =
getParent();
while (c != null && (c
instanceof Frame) == false)
c
= c.getParent();
// Cast Container to
Frame
if (c instanceof Frame)
{
Frame f
= (Frame) c;
...
}
}
In the second cause, access AppletContext
using the
Applet.getAppletContext()
method as shown in the
following code:
public void
foo()
{
....
AppletContext
ac = (AppletContext) getAppletContext();
...
}
None.