java.lang.ClassCastException Thrown From the AWT Event Dispatching Thread When Mouse Moves Over an Applet Frame

Symptoms

While 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).

Cause

This exception has two possible causes:

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 the Applet.getAppletContext() method as shown in the following code:

        public void foo()
    {
         ....
         AppletContext ac = (AppletContext) getAppletContext();
         ...
    }

 

Related Information

        None.


Copyright © 1993, 2011, Oracle and/or its affiliates. All rights reserved.