The GUI in Java processes the interactions with user via mouse; keyboard and various user controls
such buttons, checkbox, text-field etc. as the events. These events are to be handled properly to
implement Java Programming as an Event-Driven Programming.
Events
Defined as an object that describes a change in state of a source object
Java defines a number of such Event Classes inside java.awt.event package
Event Classes Description
ActionEvent Generated when a button is clicked, List item is double clicked, Menu Items is
selected
AdjustmentEvent Generated when a Scroll Bar is manipulated
ComponentEvent Generated when a Java Components is hidden,moved, resized, or becomes
visible
ContainerEvent Generated when a component is added to or removed from Containers
FocusEvent Generated when a component gains or looses keyboard focus
InputEvent Abtract super class for all components input event classes such as KeyEvent
and MouseEvent
ItemEvent Generated when a checkbox or item list is clicked and also occurs when a
choice selection is made or a checkable menu item is selected or deselected
KeyEvent Generated when input is received from the keyboard
MouseEvent Generated when the mouse is dragged, moved, clicked, pressed, released;
also occurs when mouse enters or exits a component
MouseWheelEvent Generated when the mouse wheel is moved
TextEvent Generated when a values of text component change
WndowEvent Generated whena window is activated, closed, deactivated, deiconified,
iconified, opened or quit
Event Source
A source is an object that generates an event
Event generation occurs when internal state of that object changes in some way
A source must register listeners in order for the listeners to receive the notifications about a
specific type of events
Event Listeners
A listener is an object that is notified when an event occurs
It has two major requirements:
o It should be registered to one more source object to receive event notification
o It must implement methods to receive and process those notifications
Java has defined a set of interfaces for receiving and processing the events under the
java.awt.event package
MyActionHandler(DelegationEventModel frme)
{
myFrame=frme;
}
DelegationEventModel(String str)
{
super(str);
myAction=new MyActionHandler(this); //creating object of MyActionHandler
setLayout(new FlowLayout());
tObj.setEnabled(false);
pnlObj.add(btnObj);
pnlObj.add(tObj);
add(pnlObj);
addKeyListener(this); //Register itslef as the key Listener for the Frame
setVisible(true);
setSize(500,200);
}
public void keyTyped(KeyEvent ke)
// Implementing the method of KeyListener Interface
{
JOptionPane.showMessageDialog(null,"Key Typed "+ke.getKeyChar());
btnObj.setVisible(true);
}
public void keyReleased(KeyEvent ke)
// Implementing the method of KeyListener Interface
{
//setTitle("Key Up");
}
public void keyPressed(KeyEvent ke)
// Implementing the method of KeyListener Interface
{
//setTitle("Key Down");
}
}
class UsingAdapter extends Frame implements ActionListener
// Implementing Action Listener within Frame itself
{
Button btnObj;
Panel pnlObj;
TextField tObj;
MyActionHandler myAction;
UsingAdapter(String str)
{
super(str);
myAction=new MyActionHandler(this); //creating object of MyActionHandler
setLayout(new FlowLayout());
btnObj=new Button("OKAY");
pnlObj=new Panel();
pnlObj.setLayout(new FlowLayout());
tObj=new TextField("Check This Out",50);
tObj.setEnabled(false);
pnlObj.add(btnObj);
pnlObj.add(tObj);
btnObj.addActionListener(this); //Registering Action Listener for the Button
add(pnlObj);
addKeyListener(myAction); //Register itslef as the key Listener for the Frame
setVisible(true);
setSize(500,200);
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
}
}
btnObj.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
// Implementing the method of ActionListener Interface
{
tObj.setText("You Just Pressed The Button");
btnObj.setVisible(false);
}
}); //Registering and implementing Anonymous Action Listener for the Button
}
}
mouse event handling in java
Example:
Java AWT components are platform-dependent i.e. components are displayed according to the view
of operating system. AWT is heavyweight i.e. its components are using the resources of OS.
The java.awt package provides classes for AWT api such as TextField, Label, TextArea, RadioButton,
CheckBox, Choice, List etc.
Awt Hierarchy Structure
Container
The Container is a component in AWT that can contain another component like buttons, text fields,
labels etc. The class that extends Container class are known as container such as Frame, Dialog and
Panel.
Window
The window is the container that has no borders and menu bars. You must use frame, dialog or
another window for creating a window.
Graphics class
The AWT supports many graphics methods. All graphics are drawn relative to a window. This can
be the main window of an applet, a child window of an applet, or a stand-alone application
window. A graphics context is encapsulated by the Graphics Class and it’s obtained two ways:
It is passed to an applet when one of its various methods, such as paint() or update(),is called.
It is returned by the getGraphics() methods of component.
The Graphics class defines a number of drawing function. Each shape can be drawn edge-only or
filled. Object are drawn and filled in the currently select graphics color, which is black by default. The
several drawing methods :
·Drawing Lines: Line are draw by means of the drawline() method:
void drawLine(int startX,int startY, int endX,endY)
CardLayout:The CardLayout object treats each component in the container as a card. Only one card
is visible at a time.
GridLayout
The GridLayout manages the components in form of a rectangular grid.
GridBagLayout
This is the most flexible layout manager class.The object of GridBagLayout aligns the component
vertically,horizontally or along their baseline without requiring the components of same size.