Header Ads Widget

Java Applet

Java Applet

What is Applet?

An applet is a Java program that can be embedded into a web page. It runs inside the web browser and works at client side. An applet is embedded in an HTML page using the APPLET or OBJECT tag and hosted on a web server.

Important points :

  1. All applets are sub-classes (either directly or indirectly) of java.applet.Applet class.
  2. Applets are not stand-alone programs. Instead, they run within either a web browser or an applet viewer. JDK provides a standard applet viewer tool called applet viewer.
  3. In general, execution of an applet does not begin at main() method.
  4. Output of an applet window is not performed by System.out.println(). Rather it is handled with various AWT methods, such as drawString().

Four methods in the Applet class gives you the framework on which you build any serious applet −

  • init − This method is intended for whatever initialization is needed for your applet. It is called after the param tags inside the applet tag have been processed.
  • start − This method is automatically called after the browser calls the init method. It is also called whenever the user returns to the page containing the applet after having gone off to other pages.
  • stop − This method is automatically called when the user moves off the page on which the applet sits. It can, therefore, be called repeatedly in the same applet.
  • destroy − This method is only called when the browser shuts down normally. Because applets are meant to live on an HTML page, you should not normally leave resources behind after a user leaves the page that contains the applet.
  • paint − Invoked immediately after the start() method, and also any time the applet needs to repaint itself in the browser. The paint() method is actually inherited from the java.awt.

A "Hello, World" Applet

Following is a simple applet named HelloWorldApplet.java −

1import java.applet.*;
2import java.awt.*;
3public class HelloWorldApplet extends Applet {
4   public void paint (Graphics g) {
5      g.drawString ("Hello World", 25, 50);
6   }
7}

Event handling

Changing the state of an object is known as an event. For example, click on button, dragging mouse etc. The java.awt.event package provides many event classes and Listener interfaces for event handling.

Components of Event Handling

Event handling has three main components,

  • Events : An event is a change in state of an object.
  • Events Source : Event source is an object that generates an event.
  • Listeners : A listener is an object that listens to the event. A listener gets notified when an event occurs.

How Events are handled?

A source generates an Event and send it to one or more listeners registered with the source. Once event is received by the listener, they process the event and then return. Events are supported by a number of Java packages, like java.utiljava.awt and java.awt.event.

Steps to handle events:

  1. Implement appropriate interface in the class.
  2. Register the component with the listener.

 

Example of Event Handling

1import java.awt.*;
2import java.awt.event.*;
3import java.applet.*;
4import java.applet.*;
5import java.awt.event.*;
6import java.awt.*;
7public class Test extends Applet implements KeyListener
8{
9    String msg="";
10    public void init() {
11        addKeyListener(this);
12    }
13    public void keyPressed(KeyEvent k){
14        showStatus("KeyPressed");
15    }
16    public void keyReleased(KeyEvent k) {
17        showStatus("KeyRealesed");
18    }
19    public void keyTyped(KeyEvent k){
20        msg = msg+k.getKeyChar();
21        repaint();
22    }
23    public void paint(Graphics g){
24        g.drawString(msg, 20, 40);
25    }
26}

Important Event Classes and Interface

Event Classes

Description

Listener Interface

ActionEvent

generated when button is pressed, menu-item is selected, list-item is double clicked

ActionListener

MouseEvent

generated when mouse is dragged, moved,clicked,pressed or released and also when it enters or exit a component

MouseListener

KeyEvent

generated when input is received from keyboard

KeyListener

ItemEvent

generated when check-box or list item is clicked

ItemListener

TextEvent

generated when value of textarea or textfield is changed

TextListener

MouseWheelEvent

generated when mouse wheel is moved

MouseWheelListener

WindowEvent

generated when window is activated, deactivated, deiconified, iconified, opened or closed

WindowListener

ComponentEvent

generated when component is hidden, moved, resized or set visible

ComponentEventListener

ContainerEvent

generated when component is added or removed from container

ContainerListener

AdjustmentEvent

generated when scroll bar is manipulated

AdjustmentListener

FocusEvent

generated when component gains or loses keyboard focus

FocusListener


Post a Comment

0 Comments