Header Ads

How To Write Your First Applet in Java

Before We Start:
Before we really start Applet coding, let us discuss few good to know features of java.

Applet is an integral part of AWT(Abstract Windows Toolkit). This is created to work with graphics.Each button,text field, check box created in AWT has its own window.And each Window takes space in primary memory.

So if your window is having lot more fields created in AWT , it is an overhead to the under laying Operating System.That is the reason why AWT  is called heavyweight component.

Later Java has introduced Swing package which is based on AWT . In Swing components are displayed using graphical methods of their containing application. So in Swing , components do not take their own window for display. Hence it becomes lightweight.


Before we learn more into Swing, we need to learn AWT. Then it will become very easy to understand the Swing.We will start with AWT Applet.


What is an Applet?

An applet is a class that is specially designed to display graphics in Web browser.Other than displaying graphics, it can be responsive to the user as well.We need to incorporate/embed the applet class to browser.


Package structure of applet:
Whats the Approach?
The below written code is used to embed a java class to web browser.

<HTML>
<HEAD>
</HEAD>
<BODY>
<div >//width ,height and core the basic attributes so we will go with that.
<applet width=300 height=300 code="firstApplet.class"> </applet>
</APPLET>
</div>
</BODY>
</HTML> 
Lets Create the Class in Java:
In our first applet we will deal with text field and a button.let's assume there will be one text box and two buttons in the applet. If we press the first button,Some message will come on the textfield and again if we click on the Second one lets say Reset, It will make the textfield blank.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class firstApplet extends Applet implements ActionListener
{
public TextField text1;
//declare textfield
public Button button1,button2;
//declare buttons
public void init()
{
text1=new TextField(20);
//create a textfield of length 20
add(text1);
//Add it to the container...for this step refer the picture drawn above.
button1=new Button("Click Me");
//Create button
add(button1);
//Add it to the container...for this step refer the picture drawn above.
button1.addActionListener(this);
//This is how we will make the button respond..I will discuss it later.This keyword is used to send the current object.
button2=new Button("Reset");
//Create button
add(button2);
//Add it to the container...for this step refer the picture drawn above.
button2.addActionListener(this);
//This is how we will make the button respond..I will discuss it later.
}
public void actionPerformed(ActionEvent event)
{
String msg="Hello from Applet";
//Create custom String message
if(event.getSource()==button1){
//Checking which button is pressed
text1.setText(msg);
//Setting textfiled with the message
}
else if(event.getSource()==button2)
//Checking which button is pressed
{
text1.setText("");
//Reset activity ..setting the textfield with null.
}
}
} 
Output:
We are done. 

  • Open the notepad and paste the below written code for Applet

<HTML>
<HEAD>
</HEAD>
<BODY>
<div >//width ,height and core the basic attributes so we will go with that.
<applet width=300 height=300 code="firstApplet.class"> </applet>
</APPLET>
</div>
</BODY>
</HTML> 

  • Save it as "firstApplet.html"
  • Open it in a Browser.
When we open the html file to a browser
After Clicking on the Click Me button

After Clicking on Reset button



Powered by Blogger.