You are on page 1of 21

Institute of Business Studies & Research®

What is AWT?

AWT stands for Abstract Window Toolkit. It is a


platform dependent API for creating Graphical User
Interface (GUI) for java programs.

AWT is rarely used now days because of its


platform dependent and heavy-weight nature. AWT
components are considered heavy weight because
they are being generated by underlying operating
system (OS). For example if you are instantiating a
text box in AWT that means you are actually asking
OS to create a text box.

1
Java AWT Hierarchy

Container:
A container is like a screen wherein we are placing
components like buttons, text fields, checkbox etc.
In short a container contains and controls the layout
of components. A container itself is a component
(shown in the above hierarchy diagram) thus we can
add a container inside container.

2
Types of containers:-
A container is a place wherein we add components
like text field, button, checkbox etc. There are four
types of containers available in AWT: Window,
Frame, Dialog and Panel. As shown in the hierarchy
diagram above, Frame and Dialog are subclasses of
Window class.

Window: An instance of the Window class has no


border and no title
Dialog: Dialog class has border and title. An
instance of the Dialog class cannot exist without an
associated instance of the Frame class.
Panel: Panel does not contain title bar, menu bar or
border. It is a generic container for holding
components. An instance of the Panel class
provides a container to which to add components.
Frame: A frame has title, border and menu bars. It
can contain several components like buttons, text
fields, scrollbars etc. This is most widely used
container while developing an application in AWT.

Java AWT Example


We can create a GUI using Frame in two ways:
1) By extending Frame class
2) By creating the instance of Frame class

3
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){}

4
public void addItemListener(ItemListener a){}

Label
It can be any user defined name used to identify
input field like textbox, textarea etc.
Label l1=new Label("uname");
Label l2=new Label("password");

TextField
This class can be used to design textbox.
TextField tf=new TextField();
OR
TextField tf=new TextField(40);

Button
This class can be used to create a push button.
Button b1=new Button("submit");
Button b2=new Button("cancel");

TextArea
This class can be used to design a textarea, which will
accept number of characters in rows and columns
format.
TextArea ta=new TextArea(5, 10);
// here 5 is no. of rows and 10 is no. of column

5
Checkbox
This class can be used to design multi selection
checkbox.
Checkbox cb1=new Checkbox("C ");
Checkbox cb2=new Checkbox("C++");
Checkbox cb3=new Checkbox("Java");
Checkbox cb4=new Checkbox("ASP.NET");

CheckboxGroup
This class can be used to design radio button. Radio
button is used for single selection.
CheckboxGroup cbg=new CheckboxGroup();
Checkbox cb=new Checkbox("male", cbg, "true");
Checkbox cb=new Checkbox("female", cbg, "false");
Note: Under one group many number of radio button
can exist but only one can be selected at a time.

Choice
This class can be used to design a drop down box with
more options and supports single selection.
Choice c=new Choice();
c.add("C Programming"); c.add("C++"); c.add("VB.NET");
c.add("Oracle"); c.add("Java"); c.add("ASP.NET");

6
List
This class can be used to design a list box with multiple
options and support multi selection.
List l=new List(3);

l.add("C Programming"); l.add("C++"); l.add("VB.NET");

l.add("Oracle"); l.add("Java"); l.add("ASP.NET");


Note: By holding clt button multiple options can be selected.

Scrollbar
This class can be used to display a scroolbar on the
window.

Scrollbar sb=new Scrollbar(type of scrollbar, initialposition,


thamb width, minmum value, maximum value);

 Initial position represent very starting point or position of thumb.

 Thumb width represent the size of thumb which is scroll on scrollbar

 Minimum and maxium value represent boundries of scrollbar

Scrollbar sb=new Scrollbar(Scrollbar.HORIZENTAL, 10, 5, 0, 100);

Type of scrollbar
There are two types of scrollbar are available;
 scrollbar.vertical
 scrollbar.horizental

7
import java.awt.*;
class LabelExample{
public static void main(String args[])
{
Frame f= new Frame("Label Example");
Label l1,l2;
l1=new Label("Enter First Number:");
l1.setBounds(50,100, 125,30);
l2=new Label("Enter Second Number:");
l2.setBounds(50,150, 125,30);
f.add(l1);
f.add(l2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
Output:

8
WAP in AWT to Input Your Name in TextField &
Print in Label.
import java.awt.*;
import java.awt.event.*;
public class LabelExample2 extends Frame implements
ActionListener
{
TextField tf; Label l,l2; Button b;
LabelExample2()
{
tf=new TextField();
tf.setBounds(50,50, 150,20);
l=new Label();
l.setBounds(50,100, 250,20);
b=new Button("Print Name");
b.setBounds(50,150,80,30);
l2=new Label();
l2.setBounds(200,200,250,50);
b.addActionListener(this);
add(b);add(tf);add(l); add(l2);
setSize(400,400);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
try
{
String name=tf.getText();
l2.setText("Your Name is:"+name);
}

9
catch(Exception ex)
{
System.out.println(ex);
}
}
public static void main(String[] args)
{
new LabelExample2();
}
}

1
0
Java AWT TextField Example with ActionListener
import java.awt.*;
import java.awt.event.*;
public class TextFieldExample extends Frame
implements ActionListener
{
TextField tf1,tf2,tf3;
Button b1,b2;
TextFieldExample()
{
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("Add");
b1.setBounds(50,200,150,50);
b1.addActionListener(this);
add(tf1);add(tf2);add(tf3);add(b1);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String s1=tf1.getText();
String s2=tf2.getText();
int a=Integer.parseInt(s1);
1
1
int b=Integer.parseInt(s2);
int c=0;
c=a+b;
String result=String.valueOf(c);
tf3.setText(result);
}
public static void main(String[] args)
{
new TextFieldExample();
}
}

1
2
import java.awt.*;
public class TextAreaExample
{
TextAreaExample(){
Frame f= new Frame("TextArea Example");
TextArea area=new TextArea("Welcome to
IBSAR");
area.setBounds(10,30, 300,300);
f.add(area);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new TextAreaExample();
}
}

1
3
WAP in AWT to Input Strings & Count the Number
of Words & Number of Characters in TextArea.
import java.awt.*;
import java.awt.event.*;
public class TextAreaExample1 extends Frame implements
ActionListener{
Label l1,l2;
TextArea area;
Button b;
TextAreaExample1(){
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 TextAreaExample1();
}
}

1
4
Java AWT Checkbox
The Checkbox class is used to create a 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".
import java.awt.*;
public class CheckboxExample
{
CheckboxExample(){
Frame f= new Frame("Checkbox Example");
Checkbox checkbox1 = new Checkbox("Java",true);
checkbox1.setBounds(100,100, 50,50);
Checkbox checkbox2 = new Checkbox("Oracle");
checkbox2.setBounds(100,150, 50,50);
f.add(checkbox1);
f.add(checkbox2);
f.setSize(400,400);
f.setLayout(null);
1
5
f.setVisible(true);
}
public static void main(String args[])
{
new CheckboxExample();
}
}

Java AWT 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.
Note[CheckboxGroup enables you to create radio buttons
in AWT. There is no special control for creating radio
buttons in AWT].

1
6
import java.awt.*;
public class CheckboxGroupExample
{
CheckboxGroupExample(){
Frame f= new Frame("CheckboxGroup Example");
CheckboxGroup cbg = new CheckboxGroup();
Checkbox checkBox1 = new Checkbox("Java", cbg, true);
checkBox1.setBounds(100,100, 50,50);
Checkbox checkBox2 = new Checkbox("Oracle", cbg,
false);
checkBox2.setBounds(100,150, 50,50);
Checkbox checkBox3 = new Checkbox("BA", cbg, false);
checkBox3.setBounds(100,200,50,50);
f.add(checkBox1); f.add(checkBox2); f.add(checkBox3);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new CheckboxGroupExample();
}
}

1
7
Java AWT Choice
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. It inherits Component class.
import java.awt.*;
public class ChoiceExample
{
ChoiceExample(){
Frame f= new Frame("Choice Example");
Font font1 = new Font("SansSerif", Font.BOLD, 18);
Choice c=new Choice();
c.setBounds(100,100, 175,75);
c.setFont(font1);
c.add("C Programming");
c.add("C++");
c.add("VB.NET");
c.add("Oracle");
c.add("Java");
f.add(c);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new ChoiceExample();
}
}

1
8
import java.awt.*;
import java.awt.event.*;
public class ChoiceExample2
{
ChoiceExample2(){
Frame f= new Frame();
final Label label = new Label();
label.setAlignment(Label.CENTER);
label.setSize(400,100);
Button b=new Button("Show");
b.setBounds(100,150,150,20);
Font font1 = new Font("SansSerif", Font.BOLD,
18);
Choice c=new Choice();
c.setBounds(100,100, 175,75);
c.setFont(font1);
c.add("C Programming");
c.add("C++");
c.add("VB.NET");
c.add("Oracle");
c.add("Java");
c.add("ASP.NET");
f.add(c);f.add(label); f.add(b);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String data = "Programming language Selected: "+
c.getItem(c.getSelectedIndex());
label.setText(data);
}
});
1
9
}
public static void main(String args[])
{
new ChoiceExample2();
}
}
Java AWT 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. It
inherits Component class.
import java.awt.*;
public class ListExample
{
ListExample(){
Frame f= new Frame();
List l1=new List(10);
l1.setBounds(100,100, 120,180);
l1.add("C Programming");
l1.add("C++");
l1.add("C#.NET");
l1.add("ASP.NET");
l1.add("VB.NET");
l1.add("DBMS");
l1.add("Oracle");
f.add(l1);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new ListExample();
}
}

2
0
2
1

You might also like