Event Handling in Java: Components of An Event: Can Be Put Under The Following Categories
Event Handling in Java: Components of An Event: Can Be Put Under The Following Categories
(Page 1 of 7 )
You are leaving for work in the morning and someone rings the doorbell.
That is an event!
In life, you encounter events that force you to suspend other activities and respond to them
immediately. In Java, events represent all activity that goes on between the user and the application.
Javas Abstract Windowing Toolkit (AWT) communicates these actions to the programs using events.
When the user interacts with a program let us say by clicking a command button, the system creates an
event representing the action and delegates it to the event-handling code within the program. This code
determines how to handle the event so the user gets the appropriate response.
In todays tutorial we are going to learn event-driven programming, the event model of Java, and the
different ways in which you can handle events.
Event Classes: The EventObject class is at the top of the event class hierarchy. It belongs to the
java.util package. While most of the other event classes are present in java.awt.event package. The
getSource() method of the EventObject class returns the object that initiated the event. The getId ()
method returns the nature of the event. For example, if a mouse event occurs, you can find out whether
the event was click, a press, a move or release from the event object. AWT provides two conceptual
types of events: Semantic and low-level events.
A component can have multiple listeners, and a listener can be removed using removeActionListener ()
method. Next question in your mind must be what is an interface?. An Interface contains constant
values and method declaration. The difference between classes and interface is that the methods in an
interface are only declared and not implemented, that is, the methods do not have a body. What is the
Need for interface? Are interfaces are used to define behavior protocols (standard behavior) that can be
implemented by any class anywhere in the class hierarchy. The java.awt.event package contains
definitions of all event classes and listener interface. The semantic listener interfaces define by AWT
for the above mentioned semantic events are:
ActionListener
AjdustmentListener
ItemListener
TextListener
The low-level event listeners are as follows:
ComponentListener
ContainerListener
FocusListener
KeyListener
MouseListener
MouseMotionListener
WindowsListener.
{
JButton b1;
// Main Method
Public static void main (String arg[])
{
MyEvent event = new MyEvent();
}
//Constructor for the event derived class
Public MyEvent()
{
Super(Window Title: Event Handling);
b1 = new Jbutton(Click Me);
//place the button object on the window
getContentPane().add(center,b1);
//Register the listener for the button
ButtonListener listen = new ButtonListener();
b1.addActionListener(listen);
//display the window in a specific size
setVisible(true);
setSize(200,200);
}
//The Listener Class
Class ButtonListener implements ActionListener
{
//Definition for ActionPerformed() method
Public void ActionPerformed(ActionEvent evt)
{
JButton source = (JButton)evt.getSource();
Source.setText(Button Has Been Clicked, Guru!);
}
}
}
the ActionEvent object is created and delegated to the registered listener object for processing. The
Listener object contains the actionPerformed() method which processes the ActionEvent In the
actionPerformed() method, the reference to the event source is retrieved using getSource() method. The
label of the button is changed to Button has been clicked, Guru! using setText() method.
Tools of the Trade: Since the ButtonListener class has been declared under MyEvent class. Therefore
ButtonListener class is an inner class.
The above sample code pretty much does the same thing as Example I only the applet starts with the
init() method first the button is added using the add() method and is registered with ActionListener().
Once the user clicks it, The event is trapped using the getSource() and delegated to the appropriate
listener. The getGraphics() method of the image class is used along with drawString() method to draw
the text given by the specified string.
Since the applet implements MouseListener and MouseMotionListener it has to provide for the
functionality of all their methods. Observe the mouseEntered() method has a small loop to ensure the
mouse entered message remains on the status bar for a while. {mospagebreak title=Key Frienldy
Codes} Keyboard Events and KeyListener: They are generated on pressing or releasing a key. The
KeyEvent class contains constants to represent the keys pressed or typed. The event listener
corresponding to these types of event is the KeyListener.
The following example puts forth key event handling. The applet contains a label and a text field. The
recently typed in character in the text field is displayed in the status bar. Example 4.
// Key events and KeyListener.
/*
<applet code = "keyTest.class" width = 400 height = 400>
</applet>
*/
Import java.awt.*;
Import java.awt.event.*;
Import java.applet.Applet;
Public class keyTest extends Applet implements KeyListener
{
Public void init()
{
Label lab = new Label ("Enter Characters :");
add(lab);
TextField tf = new TextField(20);
add(tf);
tf.addKeyListener(this);
}
Public void keyPressed(KeyEvent e)
{}
Public void keyReleased(KeyEvent e)
{}
Public void keyTyped(KeyEvent e)
{
showStatus(" Recently typed characters are : " + e.getKeyChar());
}
}
Here the text field delegates its key events to the applet. The add() method adds the components Label
and TextField to the applet.