AWT (Abstract window toolkit) is an API to develop a
window-based application.
It is platform independent i.e. Components are displayed
according to the view of the operating system.AWT is heavyweight because its
component are using the resources of the
operating system. The java.awt package provides classes for
AWT api such as TextField, Label, TextArea,
RadioButton, CheckBox, Choice, List etc.
AWT HIERARCHY.
Container:
The container is a component in AWT that contain another
component int it.Like button ,labels etc.The classes that extend For it are are
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.
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.
|
Java AWT Example:
To create simple awt example, you need a frame
.there are two ways to create a frame in Awt
1)By extending Frame class(inheritance)
2)By creating the object of Frame class(association)
Example:(extending frame class).
package hola;
import java.awt.Button;
import java.awt.Frame;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Hola extends Frame {
Hola(){
Button
b=new Button("click me");
b.setBounds(30,100,80,30);
add(b);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public
static void main(String[] args) {
Hola
f=new Hola();
}
}
Output:
Example:(Creating an object of frame
class).
package hola;
import java.awt.Button;
import java.awt.Frame;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Hola {
Hola(){
Frame
f=new Frame();
Button
b=new Button("click me");
b.setBounds(30,100,80,30);
f.add(b);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public
static void main(String[] args) {
Hola
f=new Hola();
}
}
Output:
Event Handling:
Changing the state of an object is known as event
Example: click on the button ,on mouse over,etc.java.awt.event package provides many event classes and Listener interfaces for event
handling.
Event Classes
|
Listener Interfaces
|
ActionEvent
|
ActionListener
|
MouseEvent
|
MouseListener and MouseMotionListener
|
MouseWheelEvent
|
MouseWheelListener
|
KeyEvent
|
KeyListener
|
ItemEvent
|
ItemListener
|
TextEvent
|
TextListener
|
AdjustmentEvent
|
AdjustmentListener
|
WindowEvent
|
WindowListener
|
ComponentEvent
|
ComponentListener
|
ContainerEvent
|
ContainerListener
|
FocusEvent
|
FocusListener
|
Example:(within a class event handling)
package hola;
import java.awt.*;
import java.awt.event.*;
public class Hola implements ActionListener {
Frame
f=new Frame();
TextField
tf;
Button b;
Hola(){
tf=new
TextField();
tf.setBounds(60,50,170,20);
Button
b=new Button("Click me");
b.setBounds(100,120, 80, 30);
b.addActionListener(this);
f.add(b);
f.add(tf);
f.setSize(300,300);
f.setVisible(true);
}
public
void actionPerformed(ActionEvent e){
tf.setText("Hello");
}
public
static void main(String[] args) {
Hola
f=new Hola();
}
}
Output:
Example:(Other class event handling)
package
hola;
import
java.awt.*;
import
java.awt.event.*;
public
class Hola extends Frame{
TextField tf;
Button b;
Hola(){
tf=new TextField();
tf.setBounds(60,50,170,20);
Button b=new Button("Click me");
b.setBounds(100,120, 80, 30);
Outer o=new Outer(this);
b.addActionListener(o);
add(b);
add(tf);
setSize(300,300);
setVisible(true);
}
public static void main(String[] args) {
Hola f=new Hola();
}
}
Outer class:
package
hola;
import
java.awt.event.*;
class Outer implements ActionListener {
Hola obj;
Outer(Hola obj){
this.obj=obj;
}
@Override
public void actionPerformed(ActionEvent ae)
{
obj.tf.setText("he hello");
}
}
Output:
Example:( anonymous class)
package hola;
import java.awt.*;
import
java.awt.event.*;
public class Hola
extends Frame{
TextField tf;
Button b;
Hola(){
tf=new TextField();
tf.setBounds(60,50,170,20);
Button b=new Button("Click me");
b.setBounds(100,120, 80, 30);
b.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent
ae) {
tf.setText("hello");
}
});
add(b);
add(tf);
setSize(300,300);
setVisible(true);
}
public static void main(String[] args) {
Hola f=new Hola();
}
}
Output:
AWT Button:
Button is a control component that has a label
and generates an event when pressed. When a button is pressed
and released, AWT sends an instance of Action Event to the button,
by calling process Event on the button.
Example:
package hola;
import java.awt.*;
import java.awt.event.*;
class Outer {
public
static void main(String[] args) {
Frame f=new Frame();
Button b=new Button("click");
b.setBounds(50, 100, 80, 30);
f.add(b);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
Output:
ActionListener on Button:
Example:
package hola;
import java.awt.*;
import java.awt.event.*;
class
Outer {
public
static void main(String[] args) {
Frame f=new Frame();
TextField tf=new TextField();
tf.setBounds(50,50,150,20);
Button b=new Button("click Here");
b.setBounds(50, 100, 60, 30);
b.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae) {
tf.setText("Good Morning");
}
});
f.add(b);
f.add(tf);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
Output:
Label:
It is used to display a single
line of read only text. User cannot edit it.
Example:
package hola;
import java.awt.*;
public class Hola {
public static void
main(String[] args) {
Frame f=new
Frame("Label Example");
Label l1;
l1=new
Label("his is Label");
l1.setBounds(50,100,80,30);
f.add(l1);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
Output:
ActionListener on Label.
Example:
package hola;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Hola extends Frame implements ActionListener {
TextField tf;Label
l;Button b1;
Hola(){
tf=new
TextField();
tf.setBounds(50,50,150,20);
l=new Label();
l.setBounds(50,50,100,30);
b1=new
Button("Click Here");
b1.setBounds(50,150,60,30);
b1.addActionListener((ActionListener)
this);
add(b1);
add(l);
add(tf);
setSize(400,400);
setVisible(true);
setLayout(null);
}
public static void
main(String[] args) {
new Hola();
}
@Override
public void
actionPerformed(ActionEvent ae) {
try{
l.setText("After Clicking");
}catch(Exception e){
System.out.println(e);
}
}
}
Output:
After clicking the button.
TextField:
TextField class is a text
component that allows the editing of a single line text.
Example:
package hola;
import java.awt.*;
class Outer {
public static
void main(String[] args) {
Frame f= new
Frame("TextField 1");
TextField
t1,t2;
t1=new
TextField("TextField 2");
t1.setBounds(50,100,
200,30);
t2=new
TextField("AWT Tutorial");
t2.setBounds(50,150, 200,30);
f.add(t1);
f.add(t2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
Output:
ActionListener on Text Field;
package hola;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Hola extends Frame implements ActionListener
{
TextField
tf1,tf2,tf3;
Button b1,b2;
Hola(){
tf1=new
TextField();
tf1.setBounds(50,50,150,20);
tf2=new
TextField();
tf2.setBounds(50,100,150,20);
tf3=new
TextField();
tf3.setBounds(50,150,150,20);
tf3.setEditable(false);
b1=new Button("+");
b1.setBounds(50,200,50,50);
b2=new
Button("*");
b2.setBounds(120,200,50,50);
b1.addActionListener(this);
b2.addActionListener(this);
add(tf1);add(tf2);add(tf3);add(b1);add(b2);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public static void
main(String[] args) {
new Hola();
}
@Override
public void
actionPerformed(ActionEvent ae) {
String
s1=tf1.getText();
String
s2=tf2.getText();
int
a=Integer.parseInt(s1);
int
b=Integer.parseInt(s2);
int c=0;
if(ae.getSource()==b1)
{
c=a+b;
}else
if(ae.getSource()==b2){
c=a*b;
}
String
result=String.valueOf(c);
tf3.setText(result);
}
}
Output:
TextArea:
package hola;
import java.awt.*;
class Outer {
public static
void main(String[] args) {
Frame f= new
Frame("TextExample");
TextArea ta1;
ta1=new
TextArea("TextArea 1");
ta1.setBounds(50,100, 300,303);
f.add(ta1);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
Output:
TextArea ActionListener:
Example:
package hola;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Hola extends Frame implements ActionListener
{
Label l1,l2;
TextArea area;
Button b;
Hola(){
l1=new
Label();
l1.setBounds(50,50,100,30);
l2=new
Label();
l2.setBounds(160,50,100,30);
area=new
TextArea();
area.setBounds(20,100,300,300);
b=new
Button("Count Words");
b.setBounds(100,400,100,30);
b.addActionListener(this);
add(l1);add(l2);add(area);add(b);
setSize(400,450);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
String
text=area.getText();
String
words[]=text.split("\\s");
l1.setText("Words: "+words.length);
l2.setText("Characters: "+text.length());
}
public static void
main(String[] args) {
new Hola();
}
}
Output:
Checkbox:
It is used to turn an
option on (true) or off (false). Clicking on a Checkbox changes its state from
"on" to "off" or from "off" to "on".
Example:
package hola;
import java.awt.*;
class Outer {
Outer(){
Frame f=new
Frame("CheckBox Example:");
Checkbox
checkbox1=new Checkbox("English");
checkbox1.setBounds(100,100,60,55);
Checkbox
checkbox2=new Checkbox("Maths");
checkbox2.setBounds(100,150,50,50);
f.add(checkbox1);
f.add(checkbox2);
f.setSize(300,
300);
f.setLayout(null);
f.setVisible(true);
}
public static
void main(String[] args) {
new Outer();
}
}
Output:
Checkbox with Item Listener:
Example:
package hola;
import java.awt.*;
class Outer {
Outer(){
Frame f=new
Frame("CheckBox Example:");
Checkbox
checkbox1=new Checkbox("English");
checkbox1.setBounds(100,100,60,55);
Checkbox
checkbox2=new Checkbox("Maths");
checkbox2.setBounds(100,150,50,50);
f.add(checkbox1);
f.add(checkbox2);
f.setSize(300,
300);
f.setLayout(null);
f.setVisible(true);
}
public static
void main(String[] args) {
new Outer();
}
}
Output:
CheckBoxGroup:
The object of CheckboxGroup
class is used to group together a set of Checkbox. At a time only
one check box button is allowed to be in "on" state and remaining
check box button in "off" state. It inherits the object class.
Example:
package hola;
import java.awt.*;
class Outer {
Outer(){
Frame f=new
Frame("CheckBox Example:");
CheckboxGroup
cbg=new CheckboxGroup();
Checkbox
checkbox1=new Checkbox("Hindi",cbg,false);
checkbox1.setBounds(100,100,60,60);
Checkbox checkbox2=new
Checkbox("Marathi",cbg,true);
checkbox2.setBounds(100,150,60,60);
f.add(checkbox1);
f.add(checkbox2);
f.setSize(300,
300);
f.setLayout(null);
f.setVisible(true);
}
public static
void main(String[] args) {
new Outer();
}
}
Output:
CheckboxGroup ItemListener:
Example:
package hola;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class Hola extends Frame implements ActionListener
{
Label l1,l2;
TextArea area;
Button b;
Hola(){
final Label label
= new Label();
label.setAlignment(Label.CENTER);
label.setSize(400,100);
CheckboxGroup
cbg=new CheckboxGroup();
Checkbox
checkbox1=new Checkbox("Hindi",cbg,false);
checkbox1.setBounds(100,100,60,55);
Checkbox
checkbox2=new Checkbox("Marathi",cbg,true);
checkbox2.setBounds(100,150,60,50);
add(checkbox1);
add(checkbox2);
add(label);
checkbox1.addItemListener(new ItemListener(){
@Override
public void
itemStateChanged(ItemEvent ie) {
label.setText("Hindi:checked");
}
});
checkbox2.addItemListener(new ItemListener(){
@Override
public void
itemStateChanged(ItemEvent ie) {
label.setText("Marathi:checked");
}
});
setSize(300, 300);
setLayout(null);
setVisible(true);
}public static void main(String[] args) {
new Hola();
}
@Override
public void
actionPerformed(ActionEvent ae) {
throw new
UnsupportedOperationException("Not supported yet."); //To change body
of generated methods, choose Tools | Templates.
}
}
Output:
Choice(Dropdown List):
The object of Choice class
is used to show popup menu of choices.
Choice selected by user is shown on the top of a menu.
Example:
package hola;
import java.awt.*;
class Outer {
Outer(){
Frame f=new
Frame("Choice Example:");
Choice c=new
Choice();
c.setBounds(100,100,75,75);
c.add("English");
c.add("Maths");
c.add("Marathi");
c.add("Science");
c.add("Social
Science");
f.add(c);
f.setSize(300, 300);
f.setLayout(null);
f.setVisible(true);
}
public static
void main(String[] args) {
new Outer();
}
}
Output:
Choice(Dropdown List) Action Listener:
Example:
package hola;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class Hola extends Frame implements ActionListener
{
Label l1,l2;
TextArea area;
Button b;
Hola(){
final Label label
= new Label();
label.setAlignment(Label.CENTER);
label.setSize(400,100);
Button b=new
Button("Show");
b.setBounds(200,100,50,20);
final Choice
c=new Choice();
c.setBounds(100,100,75,75);
c.add("English");
c.add("Maths");
c.add("Marathi");
c.add("Science");
c.add("Social
Science");
add(c);add(label);add(b);
setSize(300, 300);
setLayout(null);
setVisible(true);
b.addActionListener(new ActionListener(){
@Override
public void
actionPerformed(ActionEvent ae) {
String
data="Subject Selected:"+c.getItem(c.getSelectedIndex());
label.setText(data);
}
});
}
public static void
main(String[] args) {
new Hola();
}
@Override
public void
actionPerformed(ActionEvent ae) {
throw new
UnsupportedOperationException("Not supported yet."); //To change body
of generated methods, choose Tools | Templates.
}
}
Output:
Java List:
The object of List class
represents a list of text items. By the help of list, user can choose either
one item or multiple items.
Example:
package hola;
import java.awt.*;
class Outer {
Outer(){
Frame f=new
Frame("Choice Example:");
List l=new List();
l.setBounds(100,100,75,75);
l.add("English");
l.add("Maths");
l.add("Marathi");
l.add("Science");
l.add("Social
Science");
f.add(l);
f.setSize(300,
300);
f.setLayout(null);
f.setVisible(true);
}
public static void
main(String[] args) {
new Outer();
}
}
Output:
Java List
Action Listener:
Example:
package hola;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class Hola extends Frame implements ActionListener
{
Label l1,l2;
TextArea area;
Button b;
Hola(){
final Label label =
new Label();
label.setAlignment(Label.CENTER);
label.setSize(500,100);
Button b=new
Button("Show");
b.setBounds(200,150,80,30);
final List
l=new List(4,false);
l.setBounds(100,100,70,70);
l.add("English");
l.add("Maths");
l.add("Marathi");
l.add("Science");
l.add("Social
Science");
final List l1=new
List(4,true);
l1.setBounds(100,200,70,70);
l1.add("First");
l1.add("Second");
l1.add("Third");
l1.add("Fourth");
l1.add("Fifth");
add(l);add(l1);add(label);add(b);
setSize(500, 500);
setLayout(null);
setVisible(true);
b.addActionListener(new ActionListener(){
@Override
public void
actionPerformed(ActionEvent ae) {
String
data="Subject Selected:"+l.getItem(l.getSelectedIndex());
data+=",standars selected is:";
for(String
frame:l1.getSelectedItems()){
data+=frame+"";
};
label.setText(data);
}
});
}
public static void
main(String[] args) {
new Hola();
}
@Override
public void
actionPerformed(ActionEvent ae) {
throw new
UnsupportedOperationException("Not supported yet."); //To change body
of generated methods, choose Tools | Templates.
}
}
Output:
ScrollBar
Example:
package hola;
import java.awt.*;
class Outer {
Outer(){
Frame f=new
Frame("Choice Example:");
Scrollbar s=new
Scrollbar();
s.setBounds(100,100,75,75);
f.add(s);
f.setSize(400,
400);
f.setLayout(null);
f.setVisible(true);
}
public static void
main(String[] args) {
new Outer();
}
}
Output:
Scrollbar
Example with AdjustmentListener
Example:
package hola;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class Hola
{
Hola(){
Frame f=new
Frame("Scrollbar Example");
final Label
label=new Label();
label.setAlignment(Label.CENTER);
label.setSize(400,100);
final Scrollbar
s=new Scrollbar();
s.setBounds(100,100,50,100);
f.add(s);f.add(label);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
s.addAdjustmentListener(new AdjustmentListener(){
@Override
public void
adjustmentValueChanged(AdjustmentEvent ae) {
label.setText("Vertical Scrollbar value is:"+s.getValue());
} });}
public static void
main(String[] args) {
new Hola();
}
}
Output:
Menu and MenuItem:
Example:
package hola;
import java.awt.*;
class Outer {
Outer(){
Frame f=new
Frame("Meanu and Meanue Item Example:");
MenuBar mb=new
MenuBar();
Menu menu=new
Menu("Menu");
Menu submenu=new
Menu("Sub menu");
MenuItem i1=new
MenuItem("1");
MenuItem i2=new
MenuItem("2");
MenuItem i3=new
MenuItem("3");
MenuItem i4=new
MenuItem("4");
MenuItem i5=new
MenuItem("5");
menu.add(i1);
menu.add(i2);
menu.add(i3);
submenu.add(i4);
submenu.add(i5);
menu.add(submenu);
mb.add(menu);
f.setMenuBar(mb);
f.setSize(400,
400);
f.setLayout(null);
f.setVisible(true);
}
public static void
main(String[] args) {
new Outer();
}
}