GUI Programing with Java
Java Provides 2 Frameworks for building GUI-based applications. Those are
AWT-(Abstract Window Toolkit)
Swing
AWT-(Abstract Window Toolkit):
• AWT (Abstract Window Toolkit) is an API to develop GUI or window-based
applications in java.
• The [Link] 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.
AWT Hierarchy
• Component:
Component is an abstract class that contains various classes such as Button,
Label,Checkbox,TextField, Menu and etc.
• Container:
The Container is a component in AWT that can contain another components like
buttons, textfields, labels etc. The Container class extends Frame and Panel.
1 GUI Programming in Java |Madhu T.
• Window:
The window is the container that have no borders and menu bars. You must use
frame for creating a window.
• Frame:
The Frame is the container that contain title bar and can have menu bars. It can
have other components like button, textfield etc.
• Panel:
The Panel is the container that doesn't contain title bar and menu bars. It can
have other components like button, textfield etc.
Commonly used Methods of Component class
Method Description
add(Component c) inserts a component on this component.
setSize(int width,int height) sets the size (width and height) of the component.
setLayout(LayoutManager m) defines the layout manager for the component.
setVisible(boolean status) changes the visibility of the component, by default false.
To create simple awt example, you need a frame. There are two ways to create a
frame in AWT.
• By extending Frame class (inheritance)
Ex:
class Example extends Frame
{
……..
}
• By creating the object of Frame class (association)
Ex:
class Example
{
Frame obj=new Frame();
……..
}
2 GUI Programming in Java |Madhu T.
A Simple AWT Example:
[Link]
import [Link].*;
public class AwtExample
{
public static void main(String[] args)
{
Frame f=new Frame();
[Link](400,400);
[Link](null);
[Link](true);
[Link]("Simple Example");
}
}
Output:
Javac [Link]
Java AwtExample
AWT Components or Elements:
• Button:
The button class is used to create a labeled button that has platform independent
implementation. The application result in some action when the button is pushed.
Syntax:
Button b=new Button(“Text");
(Or)
Button b1,b2;
b1=new Button(“Text”);
[Link](50,100,80,30);
setBounds(int x,int y,int width,int height)
This method is used to declare location, width & height of all components of
AWT. X Y
Example: setBounds(50,100,80,30);
width Height
3 GUI Programming in Java |Madhu T.
Label:
The Label class is a component for placing text in a container. It is used to display
a single line of read only text. The text can be changed by an application but a user
cannot edit it directly.
Syntax:
Label l1=new Label(“Text”);
(or)
Label l1,l2;
l1=new Label(“Text”);
TextField:
The TextField class is a text component that allows the editing of a single line
text.
Syntax:
TextField t1=new TextField(“Text”);
(or)
TextField t1,t2;
t1=new TextField(“Text”);
• TextArea :
The TextArea class is a multi line region that displays text. It allows the editing of
multiple line text.
Syntax:
TextArea t1=new TextArea(“Text”);
(or)
TextArea t1,t2;
t1=new TextArea(“Text”);
• 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".
Syntax:
Checkbox c1=new Checkbox(“Text”);
(or)
Checkbox c1,c2;
c1=new Checkbox(“Text”);
4 GUI Programming in Java |Madhu T.
• Choice :
The Choice class is used to show popup menu of choices. Choice selected by user
is shown on the top of a menu.
Syntax:
Choice c=new Choice();
[Link]("Item 1");
[Link]("Item 2");
[Link]("Item 3");
• List :
The List class represents a list of text items. By the help of list, user can choose
either one item or multiple items.
Syntax:
List ls=new List(Size);
[Link]("Item 1");
[Link]("Item 2");
[Link]("Item 3");
AWT does allow the user to close window directly…
We need code to close window
//Code for Close window
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we)
{
[Link](0);
}
});
Note: We need to import one package “[Link].*”.
5 GUI Programming in Java |Madhu T.
Example Programs for AWT Components:
Example: An example for Button Component in AWT.
[Link]
import [Link].*;
import [Link].*;
class ButtonExample
{
public static void main(String[] args)
{
// creation of Frame
Frame f=new Frame();
[Link](400,400);
[Link](null);
[Link](true);
[Link]("ButtonExample");
//creation of Button
Button b=new Button("SUBMIT");
[Link](150,200,95,30);
[Link](b);
//Code for Close window
[Link](new WindowAdapter(){
public void windowClosing(WindowEvent we)
{
[Link](0);
}
});
}
}
Output:
javac [Link]
java ButtonExample
6 GUI Programming in Java |Madhu T.
Example: An example for Label Component in AWT.
[Link]
import [Link].*;
import [Link].*;
class LabelExample
{
public static void main(String args[])
{
Frame f= new Frame();
[Link](400,400);
[Link](null);
[Link](true);
[Link]("Label Example");
Label l1,l2;
l1=new Label("User Name :");
[Link](50,100, 100,30);
l2=new Label("Password :");
[Link](50,150, 100,30);
[Link](l1);
[Link](l2);
//Code for Close window
[Link](new WindowAdapter(){
public void windowClosing(WindowEvent we)
{
[Link](0);
}
});
}
}
Output:
Javac [Link]
Java LabelExample
7 GUI Programming in Java |Madhu T.
Example: An example for TextField Component in AWT.
[Link]
import [Link].*;
import [Link].*;
class TextFieldExample
{
public static void main(String args[]){
Frame f= new Frame();
[Link](400,400);
[Link](null);
[Link](true);
[Link]("TextField Example");
TextField t1,t2;
t1=new TextField("");
[Link](50,100, 200,30);
t2=new TextField("");
[Link](50,150, 200,30);
[Link](t1);
[Link](t2);
//Close window
[Link](new WindowAdapter(){
public void windowClosing(WindowEvent we)
{
[Link](0);
}
});
}
}
Output:
Javac [Link]
Java TextFiledExample
8 GUI Programming in Java |Madhu T.
Example: An example for TextArea Component in AWT.
[Link]
import [Link].*;
import [Link].*;
public class TextAreaExample
{
public static void main(String args[])
{
Frame f= new Frame();
[Link](400,400);
[Link](null);
[Link](true);
[Link]("TextAreaExample");
TextArea area=new TextArea();
[Link](50,50, 300,150);
[Link](area);
//Close window
[Link](new WindowAdapter(){
public void windowClosing(WindowEvent we)
{
[Link](0);
}
});
}
}
Output:
Javac [Link]
Java TextFieldExample
9 GUI Programming in Java |Madhu T.
Example: An example for Checkbox Component in AWT.
[Link]
import [Link].*;
import [Link].*;
public class CheckboxExample
{
public static void main(String args[])
{
Frame f= new Frame("Checkbox Example");
[Link](400,400);
[Link](null);
[Link](true);
[Link]("Checkbox Example");
Checkbox chb1 = new Checkbox("DS C++");
[Link](100,100, 100,50);
Checkbox chb2 = new Checkbox("Java", true);
[Link](100,150, 50,50);
[Link](chb1);
[Link](chb2);
//Close window
[Link](new WindowAdapter(){
public void windowClosing(WindowEvent we)
{
[Link](0);
}
});
}
}
Output:
Javac [Link]
Java CheckboxExample
10 GUI Programming in Java |Madhu T.
Example: An example for Choice Component in AWT.
[Link]
import [Link].*;
import [Link].*;
public class ChoiceExample
{
public static void main(String args[])
{
Frame f= new Frame();
[Link](400,400);
[Link](null);
[Link](true);
[Link]("ChoiceExample");
Label l=new Label("Country :");
[Link](50,100,75,75);
Choice c=new Choice();
[Link](120,125,75,75);
[Link]("India");
[Link]("Japan");
[Link]("Austraila");
[Link]("U.S.A");
[Link]("U.K");
[Link](c);
[Link](l);
//code for close window
[Link](new WindowAdapter(){
public void windowClosing(WindowEvent we)
{
[Link](0);
}
});
}
}
Output:
Javac [Link]
Java ChoiceExample
11 GUI Programming in Java |Madhu T.
Example: An example for List Component in AWT.
[Link]
import [Link].*;
import [Link].*;
public class ListExample
{
public static void main(String args[])
{
Frame f= new Frame();
[Link](400,400);
[Link](null);
[Link](true);
List l1=new List(5);
[Link](100,100, 100,75);
[Link]("Item 1");
[Link]("Item 2");
[Link]("Item 3");
[Link]("Item 4");
[Link]("Item 5");
[Link](l1);
//Code for Close window
[Link](new WindowAdapter(){
public void windowClosing(WindowEvent we)
{
[Link](0);
}
});
}
}
Output:
Javac [Link]
Java ListExample
12 GUI Programming in Java |Madhu T.
Example: An example program for Login page in AWT.
[Link]
import [Link].*;
import [Link].*;
class LoginExample
{
public static void main(String args[])
{
Frame f= new Frame ("Login Page");
[Link](400,400);
[Link](null);
[Link](true);
Label l1,l2;
TextField t1,t2;
Checkbox cb;
Button b;
l1=new Label("User Name :");
[Link](50,60,100,30);
t1=new TextField("");
[Link](150,60, 200,30);
l2=new Label("Password :");
[Link](50,120,100,30);
t2=new TextField("");
[Link](150,120, 200,30);
cb=new Checkbox("Save Password");
[Link](50,150,200,30);
b=new Button("Login");
[Link](150,180,100,30);
[Link](l1); [Link](l2);
[Link](t1); [Link](t2);
[Link](cb);
[Link](b);
}
}
Output:
Javac [Link]
Java LoginExample
13 GUI Programming in Java |Madhu T.
Example: An example program for Registration page in AWT.
[Link]
import [Link].*;
import [Link].*;
class RegistrationExample
{
public static void main(String args[])
{
Frame f= new Frame();
[Link](600,800);
[Link](null);
[Link](true);
[Link]("Registration Page");
Label l1,l2,l3,l4,l5,l6;
TextField t1,t2;
Choice ch;
List ls;
Checkbox cb1,cb2,cb3;
TextArea ta;
Button b;
l1=new Label("Name :");
[Link](50,60,100,30);
t1=new TextField("");
[Link](150,60, 200,25);
l2=new Label("Regd No :");
[Link](50,120,100,30);
t2=new TextField("");
[Link](150,120, 200,25);
l3=new Label("Year :");
[Link](50,180,100,30);
ch=new Choice();
[Link](150,180,200,30);
[Link]("I-YEAR");
[Link]("II-YEAR");
[Link]("III-YEAR");
[Link]("IV-YEAR");
l4=new Label("Branch :");
14
GUI Programming in Java
[Link](50,240,100,30);
ls=new List(4);
[Link](150,240,200,80);
[Link]("CSE");
[Link]("CSIT");
[Link]("ECE");
[Link]("EEE");
[Link]("CIVIL");
[Link]("MECH");
l5=new Label("Subjects :");
[Link](50,330,100,30);
cb1=new Checkbox("C");
[Link](150,330,30,30);
cb2=new Checkbox("JAVA");
[Link](190,330,50,30);
cb3=new Checkbox("DBMS");
[Link](240,330,100,30);
l6=new Label("Address :");
[Link](50,360,100,30);
ta=new TextArea();
[Link](150,360,230,120);
b=new Button("Submit");
[Link](150,520,100,30);
[Link](l1);[Link](l2);[Link](l3);
[Link](l4);[Link](l5);[Link](l6);
[Link](t1);[Link](t2);
[Link](ch);
[Link](ls);
[Link](cb1);[Link](cb2);[Link](cb3);
[Link](ta);
[Link](b);
}
}
Output:
Javac [Link]
Java RegistrationExample
15
GUI Programming in Java
Layout Managers
In Java, Layout Managers is used for arranging the components in order.
Layout Manager is an interface that is implemented by all the classes of layout
managers.
There are following classes that represents the layout managers
[Link]
[Link]
[Link]
[Link]
BorderLayout:
The BorderLayout is used to arrange the components in five regions: NORTH, SOUTH,
EAST, WEST and CENTER.
Each region (area) may contain one component only. It is the default layout of frame
or window.
Example Program:
import [Link].*;
public class BorderLayoutDemo {
public static void main(String[] args) {
Frame f = new Frame("BorderLayout");
Button b1=new Button("NORTH");
Button b2=new Button("SOUTH");
Button b3=new Button("EAST");
Button b4=new Button("WEST");
Button b5=new Button("CENTER");
[Link](b2,[Link]);
[Link](b1,[Link]);
[Link](b3,[Link]);
[Link](b4,[Link]);
[Link](b5,[Link]);
16
GUI Programming in Java
[Link](300,300);
[Link](true);
}
}
Output Screenshot:
FlowLayout
The FlowLayout is used to arrange the components in a line, one after another (in a
flow).
Fields of FlowLayout are LEFT,RIGHT and CENTER.
Example Program:
import [Link].*;
public class FlowLayoutDemo {
public static void main(String[] args) {
Frame f = new Frame("FlowLayout");
Button b1=new Button("1");
Button b2=new Button("2");
17
GUI Programming in Java
Button b3=new Button("3");
Button b4=new Button("4");
Button b5=new Button("5");
[Link](b1);
[Link](b2);
[Link](b3);
[Link](b4);
[Link](b5);
[Link](new FlowLayout([Link]));
[Link](300,300);
[Link](true);
}
}
Output Screenshot:
18
GUI Programming in Java
GridLayout:
The GridLayout is used to arrange the components in rectangular grid.
One component is displayed in each rectangle.
Example Program:
import [Link].*;
public class GridLayoutDemo {
public static void main(String[] args) {
Frame f = new Frame("GridLayout");
Button b1=new Button("1");
Button b2=new Button("2");
Button b3=new Button("3");
Button b4=new Button("4");
Button b5=new Button("5");
Button b6=new Button("6");
[Link](b1);
[Link](b2);
[Link](b3);
[Link](b4);
[Link](b5);
[Link](b6);
[Link](new GridLayout(2,3));
[Link](300,300);
[Link](true);
}
}
Output Screenshot:
19
GUI Programming in Java
CardLayout:
The CardLayout class manages the components in such a manner that only
one component is visible at a time.
It treats each component as a card that is why it is known as CardLayout.
Example Program:
import [Link].*;
public class CardLayoutDemo {
public static void main(String[] args) {
Frame f = new Frame("CardLayout");
Button b1=new Button("1");
Button b2=new Button("2");
Button b3=new Button("3");
[Link](b1);
[Link](b2);
[Link](b3);
[Link](new CardLayout(50,20));
[Link](300,300);
[Link](true);
}
}
Output Screenshot:
20
GUI Programming in Java
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 have the code which is known as event handler that is executed
when an event occurs.
The [Link] package provides many event classes and Listener interfaces for
event handling.
Event Classes Description Listener Interface
ActionEvent generated when button is ActionListener
pressed, menu-item is
selected, list-item is double
clicked
MouseEvent generated when mouse is MouseListener
dragged,
moved,clicked,pressed or
released and also when it
enters or exit a component
KeyEvent generated when input is KeyListener
received from keyboard
21
GUI Programming in Java
ItemEvent generated when check-box ItemListener
or list item is clicked
TextEvent generated when value of TextListener
textarea or textfield is
changed
MouseWheelEvent generated when mouse MouseWheelListener
wheel is moved
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”);
[Link](100,50,80,30);
[Link](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)
{
[Link](“welcome”);
}
22
GUI Programming in Java
Example Program for Action Listener:
import [Link].*;
import [Link].*;
class EventExample extends Frame implements ActionListener{
TextField tf;
EventExample(){
tf=new TextField(); //create components
[Link](60,50,170,20);
Button b=new Button("click me");
[Link](100,120,80,30);
[Link](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){
[Link]("Welcome");
}
public static void main(String args[]){
new EventExample();
}
}
Output: javac [Link]
java EventExample
23
GUI Programming in Java
Example Program for Key Listener
import [Link].*;
import [Link].*;
public class KeyListenerExample extends Frame implements KeyListener{
Label l;
TextArea area;
KeyListenerExample(){
l=new Label();
[Link](20,50,100,20);
area=new TextArea();
[Link](20,80,200, 200);
[Link](this);
add(l);add(area);
setSize(400,400);
setLayout(null);
setVisible(true);
}
public void keyPressed(KeyEvent e) {
[Link]("Key Pressed");
}
public void keyReleased(KeyEvent e) {
[Link]("Key Released");
}
public void keyTyped(KeyEvent e) {
[Link]("Key Typed");
}
public static void main(String[] args) {
new KeyListenerExample();
}
}
Output: javac [Link]
java KeyListenerExample
24
GUI Programming in Java
Example Program for MouseListener
import [Link].*;
import [Link].*;
public class MouseListenerExample extends Frame implements MouseListener{
Label l;
MouseListenerExample(){
addMouseListener(this);
l=new Label();
[Link](20,50,100,20);
add(l);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void mouseClicked(MouseEvent e) {
[Link]("Mouse Clicked");
}
public void mouseEntered(MouseEvent e) {
[Link]("Mouse Entered");
}
public void mouseExited(MouseEvent e) {
[Link]("Mouse Exited");
}
public void mousePressed(MouseEvent e) {
[Link]("Mouse Pressed");
}
public void mouseReleased(MouseEvent e) {
[Link]("Mouse Released");
}
public static void main(String[] args) {
new MouseListenerExample();
}
}
25
GUI Programming in Java
Output:
javac [Link]
java MouseListenerExample
26
GUI Programming in Java