Very Simple Applet Problem

Thread Solved

Join Date: Sep 2008
Posts: 83
Reputation: SoulMazer is an unknown quantity at this point 
Solved Threads: 1
SoulMazer SoulMazer is offline Offline
Junior Poster in Training

Very Simple Applet Problem

 
1
  #1
Oct 10th, 2009
Ok, well I am JUST beginning to learn Java, and I have run into a little stumbling block. I am attempting to write a little applet, but it doesn't seem to want to run.

My Java code:
  1. import java.awt.*;
  2. import java.applet.*;
  3.  
  4. public class TestApplet extends Applet {
  5. public void paint(Graphics g) {
  6. g.drawString("Please work my little applet!", 20, 30);
  7. }
  8. }

My HTML:
  1. <HTML>
  2. <HEAD><TITLE>Test Applet</TITLE></HEAD>
  3. <BODY>
  4. <applet code="TestApplet.class" width="300" height ="300">
  5. </BODY>
  6. </HTML>

So...I'm not quite sure where I am going wrong. I receive no errors while compiling and there isn't much code for me to mess up.

Could anybody help me with this problem?

Thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 3,584
Reputation: jasimp has a spectacular aura about jasimp has a spectacular aura about jasimp has a spectacular aura about 
Solved Threads: 51
Featured Poster
jasimp's Avatar
jasimp jasimp is offline Offline
Senior Poster
 
0
  #2
Oct 10th, 2009
Read the Java tutorial on applets, here. You are missing a key method every applet needs.
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 83
Reputation: SoulMazer is an unknown quantity at this point 
Solved Threads: 1
SoulMazer SoulMazer is offline Offline
Junior Poster in Training
 
0
  #3
Oct 10th, 2009
Thanks for the link, except that example is not even working. I copied and pasted the code verbatim and I still get a little gray box of nothing.

Thanks again.

EDIT: Well, this is odd. I am able to view my applet within an IDE but not through a browser (Firefox 3.5). Any ideas to why this is?
Last edited by SoulMazer; Oct 10th, 2009 at 8:56 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 3,584
Reputation: jasimp has a spectacular aura about jasimp has a spectacular aura about jasimp has a spectacular aura about 
Solved Threads: 51
Featured Poster
jasimp's Avatar
jasimp jasimp is offline Offline
Senior Poster
 
0
  #4
Oct 11th, 2009
Originally Posted by SoulMazer View Post
EDIT: Well, this is odd. I am able to view my applet within an IDE but not through a browser (Firefox 3.5). Any ideas to why this is?
Not really lol. I never had to integrate them with webpages, I just had to make some for a class once. I'm sure someone else around here will know.
Last edited by jasimp; Oct 11th, 2009 at 12:57 am.
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 120
Reputation: Jocamps is an unknown quantity at this point 
Solved Threads: 7
Jocamps's Avatar
Jocamps Jocamps is offline Offline
Junior Poster
 
2
  #5
Oct 11th, 2009
There are a couple of things you can check for this problem:
1. Is your browser set to "Enable Java" ? (Go to Tools > Options > Content Panel
2. Ensure that your applet file (class file) have the appropriate file-access permission and the browser should be able to read it (read/write access to files)
3. Read on security policies that go with your applets when deploying it to a browser.
2. Lastly, Did you get any error messages inside the browser?

hope that helps
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 83
Reputation: SoulMazer is an unknown quantity at this point 
Solved Threads: 1
SoulMazer SoulMazer is offline Offline
Junior Poster in Training
 
0
  #6
Oct 11th, 2009
1. Yes, Java is enabled.
2. Both of the files have read/write/execute access.
3. Sorry, could you explain this a little bit?
4. No, I received no error messages in Linux or Windows.

If you would like to see this for yourself, (in no way am I advertising my website) you can see it here: http://patbriggs.net/mytest.html

Thanks for the ideas.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 203
Reputation: llemes4011 is an unknown quantity at this point 
Solved Threads: 13
llemes4011 llemes4011 is offline Offline
Posting Whiz in Training
 
0
  #7
Oct 12th, 2009
Hello! Most Applets now extend the JApplet class now. (JApplet is part of the javax.swing package) So you should probably be extending that instead. also, all applets make a call to init(), start(), destroy(), and stop(). The paint() method is located in the Container class. anyways, that's just some background info for applets. You should add in the above methods to your code, because they're called by the browser to start the applet. hope this helps =)
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 120
Reputation: Jocamps is an unknown quantity at this point 
Solved Threads: 7
Jocamps's Avatar
Jocamps Jocamps is offline Offline
Junior Poster
 
0
  #8
Oct 12th, 2009
Actually, when I opened your link there was an error message displayed in the java console. If you are using Mozilla you can go to Tools > Java Console. If you are using Internet Explorer , right click on the applet area and select Java Console.

The error was a 'java.lang.NoClassDefFoundError' which is caused when your browser couldn't locate the *.class file of your applet. There are two options you can go about solving this:
1. make sure all your *.class files are in the same directory as to your html file; or
2. use CODEBASE attribute of the APPLET tag in your html file(point the codebase to the directory of where your *.class files of the applet are located)

About the security thing read it here:

http://java.sun.com/developer/online...ok/signed.html

hope that helps
Last edited by Jocamps; Oct 12th, 2009 at 1:01 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 83
Reputation: SoulMazer is an unknown quantity at this point 
Solved Threads: 1
SoulMazer SoulMazer is offline Offline
Junior Poster in Training
 
0
  #9
Oct 12th, 2009
Ok, well it's rather hard for me to test as whenever I visit the page with my applet on it, my browser crashes (so all I know is that it doesn't work).

And both my .class file and my .html file are already in the same directory. I have also added a "codebase" element to my applet tag.

Updated code:
  1. <HTML>
  2. <HEAD>
  3. <TITLE>Test Applet</TITLE>
  4. </HEAD>
  5. <BODY>
  6. <applet codebase="/" code="TestApplet.class" width="300" height ="300">
  7. </applet>
  8. </BODY>
  9. </HTML>

Thanks for all the great help. But have any more ideas?
Last edited by SoulMazer; Oct 12th, 2009 at 1:18 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 83
Reputation: SoulMazer is an unknown quantity at this point 
Solved Threads: 1
SoulMazer SoulMazer is offline Offline
Junior Poster in Training
 
0
  #10
32 Days Ago
Okay, I would like to start by say I'm sorry if this forum has a strict "no-bump after x days" etiquette, but I found a solution to my problem and I thought I would share. Although I did successfully have the latest JRE installed, I guess I didn't have a Java plugin installed for my browser. Being on Linux, I have no idea why my package manager would install only the JRE and not the browser plugin...oh well.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC