java3rd
java3rd
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.
Component class: Component class is at the top of AWT hierarchy. Component is an abstract
class that encapsulates all the attributes of visual component. A component object is
responsible for remembering the current foreground and background colors and the currently
selected text font.
Container: Container is a component in AWT that contains another component like button, text
field, tables etc. Container is a subclass of component class. Container class keeps track of
components that are added to another component.
Window: The window is a container that has no borders and menu bars. You must use frame,
dialog or another window for creating a window.
Panel: Panel class is a concrete subclass of Container. Panel does not contain title bar, menu
bar or border. It is container that is used for holding components. It can have other
components like button, textfield etc.
Frame: Frame is a subclass of Window and has resizing canvas. It is a container that contains
several different components like button, title bar, textfield, label etc. In Java, most of the AWT
applications are created using Frame window. Frame class has two different constructors,
Syntax:
Creating a Frame
import java.awt.*;
Testawt()
package testawt;
import java.awt.*;
import java.awt.event.*;
public Testawt()
}
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 m) defines the layout manager for the component.
public void setVisible(boolean status) changes the visibility of the component, by default false.
AWT UI Elements:
Following is the list of commonly used controls while designed GUI using AWT.
Types of Event
Foreground Events - Those events which require the direct interaction of user.
Example: clicking on a button, moving the mouse, scrolling the page etc.
Background Events - Those events that require the interaction of end user are known as
background events. Example: Operating system interrupts, hardware or software failure, etc.
Event Handling
Event Handling is the mechanism that controls the event and decides what should happen if an
event occurs. This mechanism has the code which is known as event handler that is executed
when an event occurs.
We can put the event handling code into one of the following places:
1. Within class
2. Other class
3. Anonymous class
Layout
Layout means the arrangement of components within the container. In other way we can say
that placing the components at a particular position within the container. The task of layouting
the controls is done automatically by the Layout Manager.
Layout Manager
The layout manager automatically positions all the components within the container. If we do
not use layout manager then also the components are positioned by the default layout
manager.
Java provides us with various layout manager to position the controls. The properties like size,
shape and arrangement varies from one layout manager to other layout manager. When the
size of the applet or the application window changes the size, shape and arrangement of the
components also changes in response i.e. the layout managers adapt to the dimensions of
appletviewer or the application window.
The layout manager is associated with every Container object. Each layout manager is an object
of the class that implements the LayoutManager interface.
Following is the list of commonly used controls while designed GUI using AWT.
1) BorderLayout: The BorderLayout arranges the components to fit in the five regions: east,
west, north, south and center.
2) CardLayout: The CardLayout object treats each component in the container as a card. Only
one card is visible at a time
3) FlowLayout: The FlowLayout is the default layout.It layouts the components in a directional
flow.
4) GridLayout: The GridLayout manages the components in form of a rectangular grid.
5) GridBagLayout: This is the most flexible layout manager class.The object of GridBagLayout
aligns the component vertically,horizontally or along their baseline without requiring the
components of same size.
Java Uses the Delegation Event Model to handle the events. This model defines the standard
mechanism to generate and handle the events.
2. Event classes: Almost every event source generates an event and is named by some Java
class. For example, the event generated by button is known as ActionEvent and that of
Checkbox is known as ItemEvent. All the events are listed in java.awt.event package. Following
list gives a few components and their corresponding listeners.
COMPONENT EVENT IT GENERATES
Button, TextField, List, Menu ActionEvent
Frame WindowEvent
Checkbox, Choice, List ItemEvent
Scrollbar AdjustmentEvent
Mouse (hardware) MouseEvent
Keyboard (hardware) KeyEvent
3. Event Listeners: The events generated by the GUI components are handled by a special
group of interfaces known as "listeners". Listener is an interface. Every component has its
own listener, say, AdjustmentListener handles the events of scrollbar. Some listeners handle
the events of multiple components. For example, ActionListener handles the events of
Button, TextField, List and Menus. Listeners are from java.awt.event package.
Following is the list of commonly used adapters while listening GUI events in AWT.
import java.awt.*;
import java.awt.event.*;
TextField tf;
AEvent(){
//create components
tf=new TextField();
tf.setBounds(60,50,170,20);
b.setBounds(100,120,80,30);
//register listener
add(b);add(tf);
setSize(300,300);
setLayout(null);
setVisible(true);
tf.setText("Welcome");
new AEvent();
public void setBounds(int xaxis, int yaxis, int width, int height); have been used in the above
example that sets the position of the component it may be button, textfield etc.
Applet in Java
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.
Applets are small Java applications that can be accessed on an Internet server, transported over
Internet, and can be automatically installed and run as a part of a web document.
Applet Life Cycle
init(): The init() method is the first method to execute when the applet is executed. Variable
declaration and initialization operations are performed in this method.
start(): The start() method contains the actual code of the applet that should run. The start()
method executes immediately after the init() method. It also executes whenever the applet is
restored, maximized or moving from one tab to another tab in the browser.
stop(): The stop() method stops the execution of the applet. The stop() method executes when
the applet is minimized or when moving from one tab to another in the browser.
destroy(): The destroy() method executes when the applet window is closed or when the tab
containing the webpage is closed. stop() method executes just before when destroy() method is
invoked. The destroy() method removes the applet object from memory.
paint(): The paint() method is used to redraw the output on the applet display area. The paint()
method executes after the execution of start() method and whenever the applet or browser is
resized.
1. public abstract void drawString(String str, int x, int y): is used to draw the specified string.
2. public void drawRect(int x, int y, int width, int height): draws a rectangle with the specified
width and height.
3. public abstract void fillRect(int x, int y, int width, int height): is used to fill rectangle with
the default color and specified width and height.
4. public abstract void drawOval(int x, int y, int width, int height): is used to draw oval with
the specified width and height.
5. public abstract void fillOval(int x, int y, int width, int height): is used to fill oval with the
default color and specified width and height.
6. public abstract void drawLine(int x1, int y1, int x2, int y2): is used to draw line between the
points(x1, y1) and (x2, y2).
7. public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer): is
used draw the specified image.
8. public abstract void drawArc(int x, int y, int width, int height, int startAngle, int
arcAngle): is used draw a circular or elliptical arc.
9. public abstract void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle): is
used to fill a circular or elliptical arc.
10. public abstract void setColor(Color c): is used to set the graphics current color to the
specified color.
11. public abstract void setFont(Font font): is used to set the graphics current font to the
specified font.
Parameters specify extra information that can be passed to an applet from the HTML page.
Parameters are specified using the HTML’s param tag.
Param Tag
The <param> tag is a sub tag of the <applet> tag. The <param> tag contains two attributes:
name and value which are used to specify the name of the parameter and the value of the
parameter respectively. For example, the param tags for passing name and age parameters
looks as shown below:
Now, these two parameters can be accessed in the applet program using the getParameter()
method of the Applet class.
getParameter() Method
The getParameter() method of the Applet class can be used to retrieve the parameters passed
from the HTML page. The syntax of getParameter() method is as follows:
Let’s look at a sample program which demonstrates the <param> HTML tag and the
getParameter() method:
import java.awt.*;
import java.applet.*;
String n;
String a;
n = getParameter("name");
a = getParameter("age");
/*
</applet>
*/