站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > JDK 5 Documentation v6.0, Java 2 SDK 英文文档

java.lang.ClassCastException thrown ... - JDK 5 Documentation v6.0, Java 2 SDK 英文文档

java.lang.ClassCastException thrown from the AWT event dispatching thread when the mouse moves over an applet frame


Symptoms

When running an applet in a browser using the Sun JRE, a ClassCastException is thrown from the AWT event dispatching thread when the mouse moves over the applet's frame. The same applet runs under the Microsoft VM.

        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)

Cause

This exception has two possible causes:

  1. To track mouse events on its frame, the applet tries to register MouseListener on the frame. In the Microsoft VM implementation, the applet's direct parent in the AWT hierarchical component tree is the frame. While this is implementation specific and subject to change, some applets rely on this in their code:

    public void foo()
    {
            Frame f = (Frame) getParent();
            ....
    }

     
    Because the Sun JRE has other containers between the applet and the frame, getParent() does not return a Frame object, and the above code results in a ClassCastException.
     
  2. In the Microsoft VM implementation, java.applet.AppletContext is implemented by the applet frame. While this is implementation specific and subject to change, some applets rely on this in their code:

    public void foo()
    {
            Frame f = (Frame) getParent();
            ....
            AppletContext ac = (AppletContext) f;
            ....
    }

    Because the Sun JRE implements AppletContext using a different object, the above code results in a ClassCastException.

Resolution

The workaround in the first case is to navigate the entire AWT hierarchical component tree from the applet to locate the frame, instead of relying on a frame being at a particular level of containment:

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;
               ...
      }
}

The workaround in the second case is to access AppletContext using the Applet.getAppletContext() method:

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

 

Related Information

        N/A