AWT tutorial
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
Java AWT Hierarchy
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.
Java AWT Hierarchy
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.
Java AWT Hierarchy
Panel
The Panel is the container that doesn't contain
title bar and menu bars. It can have other
components like button, textfield etc.
Java AWT Hierarchy
Frame
The Frame is the container that contain title bar
and can have menu bars. It can have other
components like button, textfield etc.
Java AWT Hierarchy
Java AWT Example
To create simple awt example, you need a
frame. There are two ways to create a frame in
AWT.
By extending Frame class (inheritance)
By creating the object of Frame class (association)
AWT Example by Inheritance
import java.awt.*;
class First extends Frame{
First(){
Button b=new Button("click me");
b.setBounds(30,100,80,30);// setting button position
add(b);//adding button into frame
setSize(300,300);//frame size 300 width and 300 height
setLayout(null);//no layout manager
setVisible(true);//now frame will be visible, by default not vis
ible
}
public static void main(String args[]){
First f=new First();
}}
The setBounds(int xaxis, int yaxis, int width, int height) method is used in the
above example that sets the position of the awt button.
AWT Example by Association
import java.awt.*;
class First2{
First2(){
Frame f=new Frame();
Button b=new Button("click me");
b.setBounds(30,50,80,30);
f.add(b);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[]){
First2 f=new First2();
}}
Event and Listener (Java 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.
Java Event classes and Listener
interfaces
Steps to perform Event Handling
Following steps are required to perform event
handling:
Register the component with the Listener
Registration Methods
For registering the component with the Listener,
many classes provide the registration methods.
For example:
Button
public void addActionListener(ActionListener a){}
MenuItem
public void addActionListener(ActionListener a){}
TextField
public void addActionListener(ActionListener a){}
public void addTextListener(TextListener a){}
TextArea
public void addTextListener(TextListener a){}
Checkbox
public void addItemListener(ItemListener a){}
Choice
public void addItemListener(ItemListener a){}
List
public void addActionListener(ActionListener a){}
public void addItemListener(ItemListener a){}
Java Event Handling Code
We can put the event handling code into one of
the following places:
Within class
Other class
Anonymous class
Java event handling by implementing ActionListener
//register listener
import java.awt.*; b.addActionListener(this);//pas
import java.awt.event.*; sing current instance
class AEvent extends Frame im
plements ActionListener{ //add components and set size
TextField tf; , layout and visibility
AEvent(){ add(b);add(tf);
setSize(300,300);
//create components setLayout(null);
tf=new TextField(); setVisible(true);
tf.setBounds(60,50,170,20); }
Button b=new Button("click me"
);
b.setBounds(100,120,80,30);
public void actionPerformed(ActionEvent e)
{
tf.setText("Welcome");
}
public static void main(String args[]){
new AEvent();
}
}
Java event handling by outer class
import java.awt.*; add(b);add(tf);
import java.awt.event.*; setSize(300,300);
class AEvent2 extends Frame{ setLayout(null);
TextField tf; setVisible(true);
AEvent2(){ }
//create components public static void main(String args[]){
tf=new TextField(); new AEvent2();
tf.setBounds(60,50,170,20); }
Button b=new Button("click me"); }
b.setBounds(100,120,80,30);
//register listener
Outer o=new Outer(this);
b.addActionListener(o);//passing outer clas
s instance
//add components and set size, layout and import java.awt.event.*;
visibility class Outer implements ActionListener{
AEvent2 obj;
Outer(AEvent2 obj){
this.obj=obj;
}
public void actionPerformed(ActionEvent
e){
obj.tf.setText("welcome");
} }
Java event handling by anonymous class
import java.awt.*; add(b);add(tf);
import java.awt.event.*; setSize(300,300);
class AEvent3 extends Frame{ setLayout(null);
TextField tf; setVisible(true);
AEvent3(){ }
tf=new TextField(); public static void main(String args[]){
tf.setBounds(60,50,170,20); new AEvent3();
Button b=new Button("click me"); }
b.setBounds(50,120,80,30); }
b.addActionListener(new ActionListener()
{
public void actionPerformed(){
tf.setText("hello");
}
});