My doubt is how to run applet ..My code is as follows: The following code complied successfully .......To see the output whether i hav 2 type it in browser or dos prompt itself.. If i gave java SampleButton while Running in dos prompt, Its giving error called exception in thread "main" java.lang.noclassDefFoundError: SampleButton/Html.
How to see its output in browser.

Thank u ....

import java.awt.*;
import java.applet.*;

public class SampleButton extends Applet {

    java.awt.Button button1;
    int presses = 0;

    public void init() {
        add( button1 = new java.awt.Button("Sample Button") );
    }

    public boolean handleEvent(Event event) {
        if (event.target == button1
        &&  event.id     == Event.ACTION_EVENT
        ){
            Button_Clicked(event);
            return true;
        }
        return super.handleEvent(event);
    }

    void Button_Clicked(Event event) {
        System.out.println("Sample Button presses == " + ++presses);           
    showStatus(        "Sample Button presses == " +   presses);
    }       
}

You can use JCreator or another IDE to generate the applet skeleton with the html page it goes in. That way you know you won't have any errors will building the applet's framework.

I saved the same file in html and in dos prompt i gave Appletviewer filename.html ...But i cannot see it browser....In browser its showing the program code...I complied tat code and got the class file in same bin directory.........

Did you read either of the links I supplied? To view in browser, you need to set up an applet tag in an HTML page and then open that page.

ya i tried but tat button is not displaying in html page

to insert applet into html documant you have to type following into your HTML

<APPLET CODE=AppletSubclass.class WIDTH=anInt HEIGHT=anInt>
</APPLET>

Taken from tutorial on Sun provided by Ezzaral

http://java.sun.com/docs/books/tutorial/deployment/applet/html.html

if you do not provide initial width and height that will be one of the reason why you do not see it. Secondly you may not code it properly. In the meaning there are no compiler errors but applet is not visible us you may omited something. So if you did set width&height and applet is still not showing then please show us your code and we can look at it

Member Avatar for iamthwee
import java.awt.*;
import java.applet.*;
public class SampleButton extends Applet
{
   java.awt.Button button1;
   int presses = 0;
   public void init()
   {
      add ( button1 = new java.awt.Button ( "Sample Button" ) );
   }
   public boolean handleEvent ( Event event )
   {
      if ( event.target == button1
            && event.id == Event.ACTION_EVENT
         )
      {
         Button_Clicked ( event );
         return true;
      }
      return super.handleEvent ( event );
   }
   void Button_Clicked ( Event event )
   {
      System.out.println ( "Sample Button presses == " + ++presses );
      showStatus ( "Sample Button presses == " + presses );
   }
}

I've not bothered to test this but the bit in red looks a little wierd, although it may be legal.

Member Avatar for iamthwee

Actually forget that it works ok.

Hmm, although I get a deprecated error msg when compiling with 1.4.2_15.

C:\j2sdk1.4.2_15\bin > javac SampleButton.java
Note:SampleButton.java uses or overrides a deprecated API.
Note:Recompile with - deprecation for details.
 
C:\j2sdk1.4.2_15\bin > java SampleButton.java - deprecation
Exception in thread "main" java.lang.NoClassDefFoundError:SampleButton / java

I guess I should update to the latest :D Dunno.

I guess I should update to the latest :D Dunno.

You not only one making mistakes today, I didn't spot code in first post :P
Code is running without any problems on 1.6.0 and no deprecated error messages. Once embeded in html it does show button and click count in status bar. html document used

<html>
<head>
<title>Untitled Document</title>
</head>

<body>
<applet code="SampleButton.class" width="100" height="100">
</applet>
</body>
</html>

Ya Peter Ur right ...I realiased my mistake ...

import java.awt.*;
import java.applet.*;
/*
<APPLET CODE=SampleButton WIDTH=230 HEIGHT=220>
</APPLET>
*/
public class SampleButton extends Applet {


    java.awt.Button button1;
    int presses = 0;

    public void init() {
        add( button1 = new java.awt.Button("Sample Button") );
    }

    public boolean handleEvent(Event event) {
        if (event.target == button1
        &&  event.id     == Event.ACTION_EVENT
        ){
            Button_Clicked(event);
            return true;
        }
        return super.handleEvent(event);
    }

    void Button_Clicked(Event event) {
        System.out.println("Sample Button presses == " + ++presses);           
    showStatus(        "Sample Button presses == " +   presses);
    }       
}

This Code is running after i inserted tags.Thank u For all ....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.