Results 1 to 7 of 7
  1. #1
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default Java applet won't display in firefox

    Hi, I am trying to get a java applet to display in firefox and it won't run. The code is from my Java book cd. I get the following error from the browser. Any help GREATLY appreciated. Thanks. Derek:D
    __________________________________________________ __________


    Java Plug-in 1.6.0_25
    Using JRE version 1.6.0_25-b06 Java HotSpot(TM) Client VM
    User home directory = C:\Documents and Settings\derek
    ----------------------------------------------------
    c: clear console window
    f: finalize objects on finalization queue
    g: garbage collect
    h: display this help message
    l: dump classloader list
    m: print memory usage
    o: trigger logging
    q: hide console
    r: reload policy configuration
    s: dump system and deployment properties
    t: dump thread list
    v: dump thread stack
    x: clear classloader cache
    0-5: set trace level to <n>
    ----------------------------------------------------


    java.lang.reflect.InvocationTargetException
    at com.sun.deploy.util.DeployAWTUtil.invokeAndWait(Un known Source)
    at sun.plugin2.applet.Plugin2Manager.runOnEDT(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager.createApplet(Unk nown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionR unnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    Caused by: java.lang.ClassCastException: PushCounter cannot be cast to java.applet.Applet
    at sun.plugin2.applet.Plugin2Manager$12.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
    Exception: java.lang.reflect.InvocationTargetException
    __________________________________________________ ____________

    Here are the contents of my html file:

    Java Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
     
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
     
    <meta name="keywords" content="" />
    <meta name="description" content="" />
     
    <style type="text/css">
     
    </style>
    </head>
    <body>
     <applet code="PushCounter.class" width="100" height="100"</applet>
    </body>
    </html>
    Here are the contents of my two .java files.

    Java Code:
    //********************************************************************
    //  PushCounter.java       Authors: Lewis/Loftus
    //
    //  Demonstrates a graphical user interface and an event listener.
    //********************************************************************
    
    import javax.swing.JFrame;
    
    public class PushCounter
    {
       //-----------------------------------------------------------------
       //  Creates the main program frame.
       //-----------------------------------------------------------------
       public static void main (String[] args)
       {
          JFrame frame = new JFrame ("Push Counter");
          frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    
          frame.getContentPane().add(new PushCounterPanel());
    
          frame.pack();
          frame.setVisible(true);
       }
    }
    and here

    Java Code:
    //********************************************************************
    //  PushCounterPanel.java       Authors: Lewis/Loftus
    //
    //  Demonstrates a graphical user interface and an event listener.
    //********************************************************************
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class PushCounterPanel extends JPanel
    {
       private int count;
       private JButton push;
       private JLabel label;
    
       //-----------------------------------------------------------------
       //  Constructor: Sets up the GUI.
       //-----------------------------------------------------------------
       public PushCounterPanel ()
       {
          count = 0;
    
          push = new JButton ("Push Me!");
          push.addActionListener (new ButtonListener());
    
          label = new JLabel ("Pushes: " + count);
    
          add (push);
          add (label);
    
          setPreferredSize (new Dimension(300, 40));
          setBackground (Color.cyan);
       }
    
       //*****************************************************************
       //  Represents a listener for button push (action) events.
       //*****************************************************************
       private class ButtonListener implements ActionListener
       {
          //--------------------------------------------------------------
          //  Updates the counter and label when the button is pushed.
          //--------------------------------------------------------------
          public void actionPerformed (ActionEvent event)
          {
             count++;
             label.setText("Pushes: " + count);
          }
       }
    }
    I have also posted this question here Java applet won't display in firefox (Beginning Java forum at JavaRanch)
    Last edited by silverglade; 05-12-2011 at 10:06 PM.

  2. #2
    cultclassic is offline Member
    Join Date
    Mar 2011
    Posts
    64
    Rep Power
    0

    Default

    Java Code:
    Caused by: java.lang.ClassCastException: PushCounter cannot be cast to java.applet.Applet
    means: PushCounter must extend java.applet.Applet

    Tutorial on applet:
    http://download.oracle.com/javase/tu...etStarted.html

  3. #3
    cultclassic is offline Member
    Join Date
    Mar 2011
    Posts
    64
    Rep Power
    0

    Default

    also,
    Java Code:
    <applet code="PushCounter.class" width="100" height="100"</applet>
    you have not closed the applet tag...

  4. #4
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    Thank you CultClassic. I was able to not get the error now, but it only displays a light gray box in the upper left corner of browser (I didn't place it). Any more help greatly appreciated. thank you. Here is my new code fixed with the extends applet and closed applet html tag.


    HTML

    Java Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
     
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
     
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <link href="default.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
     
    </style>
    </head>
    <body>
     <applet code="PushCounter.class" width="100" height="100"codebase="../pushcounter/"></applet>
    </body>
    </html>
    Java file 1 (which I compile into a .class file)

    Java Code:
    //********************************************************************
    //  PushCounter.java       Authors: Lewis/Loftus
    //
    //  Demonstrates a graphical user interface and an event listener.
    //********************************************************************
    
    import javax.swing.JApplet;
    import javax.swing.JFrame;
    
    public class PushCounter extends JApplet
    {
       //-----------------------------------------------------------------
       //  Creates the main program frame.
       //-----------------------------------------------------------------
       public static void main (String[] args)
       {
          JFrame frame = new JFrame ("Push Counter");
          frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    
          frame.getContentPane().add(new PushCounterPanel());
    
          frame.pack();
          frame.setVisible(true);
       }
    }
    Java file 2

    Java Code:
    //********************************************************************
    //  PushCounterPanel.java       Authors: Lewis/Loftus
    //
    //  Demonstrates a graphical user interface and an event listener.
    //********************************************************************
    import javax.swing.JApplet;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class PushCounterPanel extends JPanel
    {
       private int count;
       private JButton push;
       private JLabel label;
    
       //-----------------------------------------------------------------
       //  Constructor: Sets up the GUI.
       //-----------------------------------------------------------------
       public PushCounterPanel ()
       {
          count = 0;
    
          push = new JButton ("Push Me!");
          push.addActionListener (new ButtonListener());
    
          label = new JLabel ("Pushes: " + count);
    
          add (push);
          add (label);
    
          setPreferredSize (new Dimension(300, 40));
          setBackground (Color.cyan);
       }
    
       //*****************************************************************
       //  Represents a listener for button push (action) events.
       //*****************************************************************
       private class ButtonListener implements ActionListener
       {
          //--------------------------------------------------------------
          //  Updates the counter and label when the button is pushed.
          //--------------------------------------------------------------
          public void actionPerformed (ActionEvent event)
          {
             count++;
             label.setText("Pushes: " + count);
          }
       }
    }

  5. #5
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    I took out all the applet references and ran it at the command line and it works. It's a desktop app, not an applet, my mistake, thank you.

  6. #6
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    Does anyone know how I can give my java app to someone on the internet and let them run it from their desktop instead of the command line please?

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,303
    Rep Power
    20

Similar Threads

  1. Signed Applet crashing FireFox in Ubuntu
    By saurabhbatra in forum Java Applets
    Replies: 1
    Last Post: 06-07-2010, 01:53 AM
  2. Java applets is not working on firefox
    By yanran336 in forum Java Applets
    Replies: 4
    Last Post: 02-18-2010, 01:19 PM
  3. display bar graph with the help of java applet
    By ras_pari in forum Java Applets
    Replies: 1
    Last Post: 12-15-2009, 01:18 PM
  4. Replies: 2
    Last Post: 04-26-2009, 06:36 AM
  5. How to force Firefox to reload applet
    By JordashTalon in forum Java Applets
    Replies: 3
    Last Post: 02-20-2009, 11:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •