Unit-4 Event and GUI Programming
Unit-4 Event and GUI Programming
UNIT – 4
EVENT AND GUI PROGRAMMING
Syllabus: Event and GUI programming: Event handling in java, Event types, Mouse and key events, GUI
Basics, Panels, Frames, Layout Managers: Flow Layout, Border Layout, Grid Layout, GUI components like
Buttons, Check Boxes, Radio Buttons, Labels, Text Fields, Text Areas, Combo Boxes, Lists, Scroll Bars, Sliders,
Windows, Menus, Dialog Box, Applet and its life cycle.
EVENT TYPES
Event describes the change in state of any object. For Ex: Pressing a button, Entering a
character in Textbox, Clicking or Dragging a mouse, etc.
Events are divided into two major categories:
Foreground Events: The foreground events are those events that require direct
interaction of the user. These types of events are generated as a result of user
interaction with the GUI component. For example, clicking on a button, mouse
movement, pressing a keyboard key, selecting an option from the list, etc.
Background Events: The Background events are those events that result from the
interaction of the end-user. For example, an Operating system interrupts system
failure (Hardware or Software).
To handle these events, we need an event handling mechanism that provides
control over the events and responses.
MOUSE EVENTS
The user may click, release, drag or move a mouse while interacting with the application
these are the mouse events. To trap the mouse events, MouseListener and
MouseMotionListener interfaces of java.awt.event package are used.
The following methods are being used by MouseListener:
KEY EVENTS
A user interacts with the application by pressing either keys on keyboard or by using
mouse. A programmer must know which key the user has pressed on the keyboard or
whether the mouse is moved, pressed or released. These are also called key events,
knowing these events will enable the programmer to write his code according to the key
pressed or mouse event.
keyListener interface of java.awt.event package helps to know which key is pressed or
released by the user.
It has 3 methods:
3. public void keyReleased(keyEvent ke): This method is called when the key
is released.
GUI BASICS
AWT stands for Abstract window toolkit is an Application Programming Interface (API) for
creating Graphical User Interface (GUI) in Java. It allows Java programmers to develop
window-based applications.
AWT provides various components like button, label, checkbox, etc. used as objects inside
a Java program.
The following image represents the hierarchy for Java AWT:
LAYOUT MANAGERS
The LayoutManagers are used to arrange components in a particular manner. The Java
LayoutManagers facilitates us to control the positioning and size of the components in GUI
1. Flow Layout:
The Java FlowLayout class is used to arrange the components in a line, one after
another (in a flow). It is the default layout of the applet or panel.
FlowLayout(int align, int hgap, int vgap): creates a flow layout with the given
alignment and the given horizontal and vertical gap.
2. Boarder Layout:
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 a frame or window. The BorderLayout provides
five constants for each region: NORTH, SOUTH, EAST, WEST, CENTER.
BorderLayout(int hgap, int vgap): creates a border layout with the given
horizontal and vertical gaps between the components.
3. Grid Layout:
The Java GridLayout class is used to arrange the components in a rectangular grid. One
component is displayed in each rectangle.
GUI COMPONENTS
GUI components or AWT controls
Java AWT controls are the components that are used to design GUI or web
applications.
AWT provides many ready-made and reusable GUI components in package
java.awt.
The frequently-used are: Button, TextField, Label, Checkbox, Radio buttons, List,
and Choice etc..
SL Component
Description GUI
No s
A Label object is a component for placing text
1 Label
in a container.
LABELS
The object of JLabel 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.
Label Declaration:
public class Label extends Component implements Accessible
Ex: Label n = new Label(“Name:”);
Java Program for creating two labels to display text on the frame.
import java.awt.*;
class demo
{
public static void main(String args[])
{
Frame f = new Frame("Label Demo");
Label lab1, lab2;
lab1 = new Label( "Welcome to JAVA World" );
lab1.setBounds(50,50,200,30);
lab2 = new Label("JAVA Programming");
lab2.setBounds(50,100,200,30);
f.add(lab1);
f.add(lab2);
f.setSize(500,500);
f.setLayout(null);
f.setVisible(true);
}
}
Output:
BUTTONS
In Java, AWT contains a Button Class. Push button is used for creating a labelled button
which can perform an action.
When a button is clicked, it generates an ActionEvent which ca be handled using the
ActionListener interface and event handling method is actionPerformed( ).
Button Declaration:
public class Button extends Component implements Accessible
Ex: Button b = new Button(String label);
Java program to create a button and add it to the frame by providing coordinates.
import java.awt.*;
TEXT FIELD
A TextField or textbox is a single line text entry control which allows the user to enter a
single line of text.
A TextField is a single line area that the user can type into. It is a good way of getting text
input from the user.
TextField Declaration:
public class TextField extends TextComponent
Ex: TextField tf = new TextField(size);
f.add(text1);
f.setSize(500,500);
f.setLayout(null);
f.setVisible(true);
}
}
TEXT AREA
A TextArea class is used for displaying multi-line text. It is a multiline region that displays
text. It allows the editing of multiple line text.
TextArea Declaration:
public class TextArea extends TextComponent
CHECKBOX
In Java, AWT contains a Checkbox Class. It is used when we want to select only one option
i.e true or false. When the checkbox is checked then its state is "on" (true) else it is
"off"(false). It permit for multiple selection.
Checkbox Syntax:
public class Checkbox extends Component implements ItemSelectable,
Accessible
import java.awt.*;
public class demo
{
demo()
{
Frame f= new Frame("Checkbox Demo");
Checkbox cb1 = new Checkbox("C++", true);
cb1.setBounds(100,100, 60,60);
Checkbox cb2= new Checkbox("JAVA”);
cb2.setBounds(100,150, 60,60);
f.add(cb1);
f.add(cb2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new demo( );
}
}
Output:
RADIO BUTTON
CheckboxGroup enables you to create radio buttons in AWT.
The object of CheckboxGroup class is used to group together a set of Checkbox. When
Checkboxes are grouped then only one box can be checked at a time. Only one check box
is in "on" state and remaining check box button is in "off" state.
Radio buttons permit only one element to be selected from the set.
CheckboxGroup Declaration:
public class CheckboxGroup extends Object implements Serializable
f.add(cb1);
f.add(cb2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new demo();
}
}
Output:
COMBO BOX
Combo box or drop down box can be created by using 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:
import java.awt.*;
public class demo
{
demo()
{
Frame f = new Frame("Combo Box Demo");
Choice obj=new Choice();
obj.setBounds(80,80, 100,100);
obj.add("Red");
obj.add("Blue");
obj.add("Black");
obj.add("Pink");
obj.add("White");
obj.add("Green");
f.add(obj);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new demo();
}
}
Output:
LIST
In Java, AWT contains a List Class. It is used to represent a list of items together. One or
more than one item can be selected from the list. More than one items in the list box are
visible to the user.
List Declaration:
import java.awt.*;
public class demolist
{
demo()
{
Frame f= new Frame("List Demo");
obj.add("Red");
obj.add("Blue");
obj.add("Black");
obj.add("Pink");
obj.add("White");
obj.add("Green");
f.add(obj);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new demo();
} Output:
}
SCROLLBAR
The object of Scrollbar class is used to add horizontal and vertical scrollbar. Scrollbar is a
GUI component allows us to see invisible number of rows and columns.
It can be added to top-level container like Frame or a component like Panel. The Scrollbar
class extends the Component class.
Scrollbar Declaration:
public class Scrollbar extends Component implements Adjustable, Accessible
import java.awt.*;
public class demo
{
demo()
{
Frame f = new Frame("Scrollbar Demo");
SLIDER
The Java JSlider class is used to create the slider. By using JSlider, a user can select a
value from a specific range.
Slider Declaration:
public class JSlider extends JComponent implements SwingConstants, Accessible
import javax.swing.*;
public class demo extends JFrame
{
demo()
{
JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 50, 25);
JPanel panel=new JPanel();
panel.add(slider);
add(panel);
}
public static void main(String s[])
{
demo frame=new demo();
frame.pack();
frame.setVisible(true);
}
} Output:
MENU
In Java, AWT contains a Menu and MenuItem Class. The menu is used to create a drop-
down of menu components which is displayed from the menu bar. MenuItem is used for
adding Lable in Menu.
Menu Declaration:
public class Menu extends MenuItem implements MenuContainer, Accessible
import java.awt.*;
import java.awt.event.*;
public class demo
{
private static Dialog d;
demo()
{
Frame f= new Frame();
d = new Dialog(f , "Dialog Demo", true);
d.setLayout( new FlowLayout() );
Button b = new Button ("OK");
b.addActionListener(new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
demo.d.setVisible(false);
}
});
d.add( new Label ("Click on button to continue."));
d.add(b);
d.setSize(300,300);
d.setVisible(true);
}
public static void main(String args[])
{
new demo();
}
}
Output:
The applet life cycle can be defined as the process of how the object is created, started,
stopped, and destroyed during the entire execution of its application. It basically has five
core methods namely init(), start(), stop(), paint() and destroy().These methods are
invoked by the browser to execute.
2. Running State:
It is the second stage of an applet in the applet life cycle. In this state, the applet
starts running. The start( ) method contains the actual code of the applet that
should run. The start() method executes immediately after the init( ) method.
Syntax: public void start( )
{
// To start the applet code
}
3. Idle State:
It is the third state of an applet in the applet life cycle. In this state, an applet
becomes idle, which means it stopped from running. stop( ) method is used to
stop an applet.
Syntax: public void stop( )
{
// code to stop the applet
}
4. Dead State:
In this state, the applet is removed from memory. destroy( ) method is used to
destroy the applet. This occurs automatically when we quit the browser.
Like initialization, destroying occurs only once in the applet’s life cycle.
Syntax: public void destroy( )
{
// destroy the applet
5. Display State:
The paint( ) method is used to redraw the output on the applet display area. The
paint() method executes after the execution of start() method and whenever the
applet or browser is resized.
Syntax: public void paint(Graphics graphics)
{
//code to paint any shapes
}
*******
UNIT – 5
I/O PROGRAMMING
Syllabus: Text and Binary I/O, Binary I/O classes, Object I/O, Random Access Files. Multithreading in java:
Thread life cycle and methods, Runnable interface, Thread synchronization, Exception handling with try catch
finally.