Java Abstract Window Toolkit
Java Abstract Window Toolkit
Java AWT is an API that contains large number of classes and methods to
create and manage graphical user interface (GUI) applications. The AWT was
designed to provide a common set of tools for GUI design that could work on a
variety of platforms.
Java Provides 2 Frameworks for building GUI-based applications. Those are
AWT-(Abstract Window Toolkit)
Swing
AWT (Abstract Window Toolkit) is an API to develop GUI or window-based
applications in java.
• The java.awt package provides classes for AWT API such as TextField, Label,
TextArea, Checkbox, Choice, List etc.
• AWT components are platform-dependent i.e., components are displayed
according to the view of operating system
import java.awt.*;
import java.awt.event.*;
class ButtonExample
{
public static void main(String[] args)
{
// creation of Frame
Frame f=new Frame();
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
f.setTitle("ButtonExample");
//creation of Button
Button b=new Button("SUBMIT");
b.setBounds(150,200,95,30);
f.add(b);
//Code for Close window
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
}
AWT does allow the user to close window directly…
We need code to close window
//Code for Close window
FrameName. addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
Note: We need to import one package “java.awt.event.*”.
TextField Example
import java.awt.*;
class TextFieldDemo1{
public static void main(String args[]){
Frame TextF_f= new Frame("TextField");
TextField text1, text2;
text1=new TextField("Welcome to JIMS");
text1.setBounds(60,100, 230,40);
text2=new TextField("This tutorial is of Java");
text2.setBounds(60,150, 230,40);
TextF_f.add(text1);
TextF_f.add(text2);
TextF_f.setSize(500,500);
TextF_f.setLayout(null);
TextF_f.setVisible(true);
}
}
AWT TextArea
TextArea Declaration:
Example:
import java.awt.*;
public class TextAreaDemo1
{
TextAreaDemo1()
{
Frame textArea_f= new Frame();
TextArea area=new TextArea("Welcome to JIMS");
area.setBounds(30,40, 200,200);
textArea_f.add(area);
textArea_f.setSize(300,300);
textArea_f.setLayout(null);
textArea_f.setVisible(true);
}
public static void main(String args[])
{
new TextAreaDemo1();
}
}
AWT Checkbox
Checkbox Syntax
Example:
In this example, we are creating checkbox that are used to get user input. If
checkbox is checked it returns true else returns false.
import java.awt.*;
public class CheckboxDemo1
{
CheckboxDemo1(){
Frame checkB_f= new Frame("Welcome to JIMS ");
Checkbox ckbox1 = new Checkbox("Yes", true);
ckbox1.setBounds(100,100, 60,60);
Checkbox ckbox2 = new Checkbox("No");
ckbox2.setBounds(100,150, 60,60);
checkB_f.add(ckbox1);
checkB_f.add(ckbox2);
checkB_f.setSize(400,400);
checkB_f.setLayout(null);
checkB_f.setVisible(true);
}
public static void main(String args[])
{
new CheckboxDemo1();
}
}
AWT Choice
In Java, AWT contains a Choice Class. It is used for creating a drop-down menu
of choices. When a user selects a particular item from the drop-down then it is
shown on the top of the menu.
Choice Declaration:
Example:
In this example, we are creating drop-down menu that is used to get user choice
from multiple choices.
import java.awt.*;
import java.awt.event.*;
public class ChoiceExample
{
public static void main(String args[])
{
Frame f= new Frame();
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
f.setTitle("ChoiceExample");
Label l=new Label("Country :");
l.setBounds(50,100,75,75);
Choice c=new Choice();
c.setBounds(120,125,75,75);
c.add("India");
c.add("Japan");
c.add("Austraila");
c.add("U.S.A");
c.add("U.K");
f.add(c);
f.add(l);
//code for close window
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
}
Example: An example for List Component in AWT.
ListExample.java
import java.awt.*;
import java.awt.event.*;
public class ListExample
{
public static void main(String args[])
{
Frame f= new Frame();
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
List l1=new List(5);
l1.setBounds(100,100, 100,75);
l1.add("Item 1");
l1.add("Item 2");
l1.add("Item 3");
l1.add("Item 4");
l1.add("Item 5");
f.add(l1);
//Code for Close window
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
}
MenuItem declaration
Menu declaration
Example:
In this example, we are creating a menu item that contains sub menu as well.
We use MenuItem and Menu class for creating menu.
import java.awt.*;
class MenuDemo1
{
MenuDemo1()
{
Frame menu_f= new Frame("Menu and MenuItem Demo");
MenuBarmenu_bar=new MenuBar();
Menu menu11=new Menu("Menu");
Menu sub_menu1=new Menu("Sub Menu =>");
MenuItem a1=new MenuItem("Red");
MenuItem a2=new MenuItem("Light Red");
MenuItem a3=new MenuItem("Drak Red");
MenuItem b1=new MenuItem("Green");
MenuItem b2=new MenuItem("Light Green");
MenuItem b3=new MenuItem("Dark Green");
menu11.add(a1);
sub_menu1.add(a2);
sub_menu1.add(a3);
menu11.add(b1);
sub_menu1.add(b2);
sub_menu1.add(b3);
menu11.add(sub_menu1);
menu_bar.add(menu11);
menu_f.setMenuBar(menu_bar);
menu_f.setSize(400,400);
menu_f.setLayout(null);
menu_f.setVisible(true);
}
public static void main(String args[])
{
new MenuDemo1();
}
}
AWT Panel
In Java, AWT contains a Panel. The panel provides a free space where
components can be placed.
Panel declaration
Example:
Lets create a panel to add components like: button, textbox etc. the panel
provides a place to add awt components.
import java.awt.*;
public class PanelDemo1{
PanelDemo1()
{
Frame panel_f= new Frame("Panel Demo");
Panel panel11=new Panel();
panel11.setBounds(40,80,200,200);
panel11.setBackground(Color.red);
Button box1=new Button("On");
box1.setBounds(50,100,80,30);
box1.setBackground(Color.gray);
Button box2=new Button("Off");
box2.setBounds(100,100,80,30);
box2.setBackground(Color.gray);
panel11.add(box1);
panel11.add(box2);
panel_f.add(panel11);
panel_f.setSize(400,400);
panel_f.setLayout(null);
panel_f.setVisible(true);
}
public static void main(String args[])
{
new PanelDemo1();
}
}
Example: An example program for Login page in AWT.
LoginExample.java
import java.awt.*;
import java.awt.event.*;
class LoginExample
{
public static void main(String args[])
{
Frame f= new Frame ("Login Page");
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
Label l1,l2;
TextField t1,t2;
Checkbox cb;
Button b;
l1=new Label("User Name :");
l1.setBounds(50,60,100,30);
t1=new TextField("");
t1.setBounds(150,60, 200,30);
l2=new Label("Password :");
l2.setBounds(50,120,100,30);
t2=new TextField("");
t2.setBounds(150,120, 200,30);
cb=new Checkbox("Save Password");
cb.setBounds(50,150,200,30);
b=new Button("Login");
b.setBounds(150,180,100,30);
f.add(l1); f.add(l2);
f.add(t1); f.add(t2);
f.add(cb);
f.add(b);
}
}
Event Handling
Event: Changing the state of an object (component) is known as an event. For
example, click on button, dragging mouse etc.
Event describes the change in state of component. Events are generated as result
of user interaction with the graphical user interface components. For example,
clicking on a button, moving the mouse, entering a character through keyboard
and selecting an item from list.
Def: 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.
The java.awt.event package provides many event classes and Listener interfaces
for event handling.
--Steps to perform Event Handling
Following steps are required to perform event handling:
– Register the component with the Listener.
By using addActionListener(ActionListener a)
Example:
Button b=new Button(“Submit”);
b.setBounds(100,50,80,30);
b.addActionListener(this);
– Provide or put event handling code.
By using actionPerformed(ActionEvent e) method of ActionListener Interface,
we can perfom action what the user want.
Example:
Public void actionPerformed(ActionEvent e)
{
tf.setText(“welcome”);
}
Simple Example:
import java.awt.*;
import java.awt.event.*;
class EventExample extends Frame implements ActionListener{
TextField tf;
EventExample(){
tf=new TextField(); //create components
tf.setBounds(60,50,170,20);
Button b=new Button("click me");
b.setBounds(100,120,80,30);
b.addActionListener(this);//register listener &passing current instance
add(b);add(tf); //add components and set size, layout and visibility
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
tf.setText("Welcome");
}
public static void main(String args[]){
new EventExample();
}}