Spec-Zone .ru
спецификации, руководства, описания, API
|
java.lang.ClassCastException
Thrown From the AWT Event Dispatching Thread When Mouse Moves Over an Applet Frame
SymptomsWhile running an applet in a browser by using the Sun JavaTM Runtime Environment (JRETM) 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).
Cause
This exception has two possible causes:
- To track mouse events on the applet's frame, the applet tries to register a
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();
....
}
But, applet's direct parent is specific to the implementation and might change. Because the Sun JRE has other containers between the applet and the frame,getParent()
method does not return aFrame
object, and the above code results injava.lang.ClassCastException
error.
- In the Microsoft VM implementation,
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 ajava.lang.ClassCastException
.Resolution
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 theApplet.getAppletContext()
method as shown in the following code:
public void foo()
{
....
AppletContext ac = (AppletContext) getAppletContext();
...
}
Related Information
None.