You are on page 1of 8

Java AWT

 Java AWT (Abstract Window Toolkit) is an API to develop GUI or window-based


applications in java.
 Java AWT components are platform-dependent i.e. components are displayed
according to the view of operating system. AWT is heavyweight i.e. its components
are using the resources of OS.
 The java.awt package provides classes for AWT api such as TextField, Label,
TextArea, RadioButton, CheckBox, Choice, List etc.

Java AWT Hierarchy


The hierarchy of Java AWT classes are given below.
Container

The Container is a component in AWT that can contain another components like buttons,
textfields, labels etc. The classes that extends Container class are known as container such as
Frame, Dialog and Panel.

Window

The window is the container that have no borders and menu bars. You must use frame, dialog
or another window for creating a window.

Panel

The Panel is the container that doesn't contain title bar and menu bars. It can have other
components like button, textfield etc.

Frame

The Frame is the container that contain title bar and can have menu bars. It can have other
components like button, textfield etc.

Useful Methods of Component class


Method Description

public void add(Component c) inserts a component on this component.

public void setSize(int width,int height) sets the size (width and height) of the component.

public void setLayout(LayoutManager


defines the layout manager for the component.
m)

changes the visibility of the component, by default


public void setVisible(boolean status)
false.
Applets in Java:-
An applet is a Java program that runs in a Web browser. An applet can be a fully functional
Java application because it has the entire Java API at its disposal.

Applet is a special type of program that is embedded in the webpage to generate the dynamic
content. It runs inside the browser and works at client side.

There are some important differences between an applet and a standalone Java application,
including the following −

 An applet is a Java class that extends the java.applet.Applet class.


 A main() method is not invoked on an applet, and an applet class will not define
main().
 Applets are designed to be embedded within an HTML page.
 When a user views an HTML page that contains an applet, the code for the applet is
downloaded to the user's machine.
 A JVM is required to view an applet. The JVM can be either a plug-in of the Web
browser or a separate runtime environment.
 The JVM on the user's machine creates an instance of the applet class and invokes
various methods during the applet's lifetime.
 Applets have strict security rules that are enforced by the Web browser. The security
of an applet is often referred to as sandbox security, comparing the applet to a child
playing in a sandbox with various rules that must be followed.
 Other classes that the applet needs can be downloaded in a single Java Archive (JAR)
file.

Advantage of Applet

There are many advantages of applet. They are as follows:

 It works at client side so less response time.


 Secured
 It can be executed by browsers running under many plateforms, including Linux,
Windows, Mac Os etc.

Drawback of Applet

 Plugin is required at client browser to execute applet.

Lifecycle of Java Applet

1. Applet is initialized.
2. Applet is started.
3. Applet is painted.
4. Applet is stopped.
5. Applet is destroyed.
Lifecycle methods for Applet:

The java.applet.Applet class 4 life cycle methods and java.awt.Component class provides 1
life cycle methods for an applet.

java.applet.Applet class

For creating any applet java.applet.Applet class must be inherited. It provides 4 life cycle
methods of applet.

1. public void init(): is used to initialized the Applet. It is invoked only once. It is called
after the param tags inside the applet tag have been processed.
2. public void start(): is invoked after the init() method or browser is maximized. It is
used to start the Applet. It is also called whenever the user returns to the page
containing the applet after having gone off to other pages.
3. public void stop(): is used to stop the Applet. It is invoked when Applet is stop or
browser is minimized. 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.
4. public void destroy(): is used to destroy the Applet. It is invoked only once. 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.

java.awt.Component class

The Component class provides 1 life cycle method of applet.


1. public void paint(Graphics g): is used to paint the Applet. It provides Graphics class
object that can be used for drawing oval, rectangle, arc etc. 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.

Ways to run an Applet:-

There are two ways to run an applet

1. By html file.
2. By appletViewer tool (for testing purpose).

1. Example of Applet by html file:

To execute the applet by html file, create an applet and compile it. After that create an html
file and place the applet code in html file. Now click the html file.

// A Hello World Applet


import java.applet.Applet;
import java.awt.Graphics;

// HelloWorld class extends Applet


public class HelloWorld extends Applet
{
// Overriding paint() method
public void paint(Graphics g)
{
g.drawString("Hello World", 20, 20);
}
}

HelloWorld.html
<html>
<body>
<applet code="HelloWorld.class" width="300" height="300">
</applet>
</body>
</html>
Explanation:

1. The above java program begins with two import statements. The first import
statement imports the Applet class from applet package. Every AWT-based(Abstract
Window Toolkit) applet that you create must be a subclass (either directly or
indirectly) of Applet class. The second statement import the Graphics class from
AWT package.
2. The next line in the program declares the class HelloWorld. This class must be
declared as public because it will be accessed by code that is outside the program.
Inside HelloWorld, paint( ) is declared. This method is defined by the AWT and must
be overridden by the applet.
3. Inside paint( ) is a call to drawString( ), which is a member of the Graphics class.
This method outputs a string beginning at the specified X,Y location. It has the
following general form:
4. void drawString(String message, int x, int y)

Here, message is the string to be output beginning at x,y. In a Java window, the upper-
left corner is location 0,0. The call to drawString( ) in the applet causes the message
“Hello World” to be displayed beginning at location 20,20.

2. Example of Applet by appletviewer tool:

To execute the applet by appletviewer tool, create an applet that contains applet tag in
comment and compile it. After that run it by: appletviewer First.java. Now Html file is not
required but it is for testing purpose only.

//First.java
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{
public void paint(Graphics g){
g.drawString("welcome to applet",150,150);
}
}

/*
<applet code="First.class" width="300" height="300">
</applet>
*/

To execute the applet by appletviewer tool, write in command prompt:

c:\>javac First.java
c:\>appletviewer First.java

3. Example Showing date in the status bar:-

import java.applet.*;// used


//to access showStatus()
import java.awt.*;//Graphic
//class is available in this package
import java.util.Date;// used
//to access Date object
public class Date1 extends Applet
{
public void paint(Graphics g)
{
Date dt = new Date();
super.showStatus("Today is" + dt);
//in this line, super keyword is
// avoidable too.
}}
/*
<applet code="Date1" width=200 height=60>
</applet>
*/

You might also like