Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-21-2008, 10:49 AM
Member
 
Join Date: Aug 2008
Posts: 1
paulsim is on a distinguished road
ClassCastException
Hi

It seems like there are thousands of people with this problem asking around on the Internet - and I don't find a decent reply.

I am trying to create a simple applet with JButtons

When compiling in JCreator I get the following message.

java.lang.ClassCastException: FramedButton cannot be cast to java.applet.Applet
at sun.applet.AppletPanel.createApplet(AppletPanel.ja va:786)
at sun.applet.AppletPanel.runLoader(AppletPanel.java: 715)
at sun.applet.AppletPanel.run(AppletPanel.java:369)
at java.lang.Thread.run(Thread.java:619)

When compiling with javax it compiles OK but does not initialise.

I have tried many examples of simple button demo applets including the one on the java site.

also this one which is similar but does not require gifs.


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ButtonDemo extends JFrame
{
protected JButton startButton, stopButton;

// Constructor sets features of the frame, creates buttons, adds them to the frame, and
// assigns an object to listen to them
public ButtonDemo()
{
super("Button demo"); // set title bar

setSize(400,200); // sets the size of the window

startButton = new JButton("Start"); // create two new buttons w/labels start and stop
stopButton = new JButton("Stop");

startButton.setBackground(Color.green); // set backgrounds of buttons
stopButton.setBackground(Color.red);

Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout()); // sets layout so objects added go from left to right
// until fill up row and then go to next row
contentPane.add(startButton);
contentPane.add(stopButton);

// create objects to listen to each button (separately):
startButton.addActionListener(new StartButtonListener());
stopButton.addActionListener(new StopButtonListener());
}


// Trivial main program associated with new window
public static void main(String[] main){
ButtonDemo app = new ButtonDemo(); // Create an instance of Buttons
app.show(); // Show it on the Mac screen
} // -- leave this out and you won't see it!

// Listener for start button
protected class StartButtonListener implements ActionListener{

// print "start button" when button pushed
public void actionPerformed(ActionEvent evt)
{
System.out.println("Start button");
}
}

// Listener for stop button
protected class StopButtonListener implements ActionListener{

// print "stop button" when button pushed
public void actionPerformed(ActionEvent evt)
{
System.out.println("Stop button");
}
}

}

I have read many short comments but no solutions.

Is my Java installed correctly?

I do have the classpath coorect to java.swing.

Please help me - and many others.

paulsim
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-21-2008, 02:13 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,545
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
First of all, I have found something on your code. I can't see that you have extend the JApplet in order to implement an applet.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-21-2008, 04:14 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
Having trouble understanding your problem.
Quote:
When compiling in JCreator I get the following message.

java.lang.ClassCastException: FramedButton cannot be cast to java.applet.Applet
This looks like an execution time error message, not a compile error.
Where is FramedButton defined? Its not in the code you posted.
Quote:
When compiling with javax
What is the javax program? Sun's java JDK uses javac to compile java programs.

Your code show a main() method. These are not used in Applets.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
java.lang.ClassCastException wrwelden New To Java 4 05-22-2008 05:43 PM
ClassCastException in TreeSet pHew New To Java 2 01-16-2008 02:20 AM
Problem with vector, java.lang.ClassCastException paul New To Java 1 07-16-2007 06:31 PM
ClassCastException Ed New To Java 2 07-04-2007 07:26 AM
ClassCastException Felissa New To Java 2 07-04-2007 07:06 AM


All times are GMT +3. The time now is 07:30 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org