Any program that uses GUI (graphical user interface) such as Java application written for
windows, is event driven. Event describes the change in state of any object. For Example
: Pressing a button, Entering a character in Textbox, Clicking or Dragging a mouse, etc.
components of Event Handling
Event handling has three main components,
Events : An event is a change in state of an object.
Events Source : Event source is an object that generates an event.
Listeners : A listener is an object that listens to the event. A listener gets notified
when an event occurs.
Event Delegation Model in Java
The modern approach to handling events is predicated on the delegation event model,
which defines standard and consistent mechanisms to get and process events. Its concept is
sort of simple: a source generates an occasion and sends it to at least one or more listeners.
In this scheme, the listener simply waits until it receives an occasion. Once an occasion is
received, the listener processes the event and then returns.
Using the delegation event model is actually quite easy. Just follow these two steps:
1. Implement the appropriate interface in the listener so that it will receive the type of event desired.
2. Implement code to register and unregister (if necessary) the listener as a recipient for the event notifications.
Advantages of using the Delegation Event Model
The advantage of this design is that the appliance logic that processes events is cleanly separated from the interface logic
that generates those events. An interface element is in a position to “delegate” the processing of an occasion to a separate
piece of code. In the delegation event model, listeners must register with a source so as to receive an occasional
notification. This provides is a crucial benefit: notifications are sent only to listeners that want to receive them. This is a more
efficient way to handle events.
How Events are handled?
A source generates an Event and send it to one or more listeners registered with the source.
Once event is received by the listener, they process the event and then return. Events are
supported by a number of Java packages, like [Link], [Link] and [Link].
important Event Classes and Interface
Event Classes Description Listener Interface
generated when button is pressed, menu-item is selected, list-
ActionEvent ActionListener
item is double clicked
generated when mouse is dragged, moved, clicked,pressed or
MouseEvent MouseListener
released and also when it enters or exit a component
KeyEvent generated when input is received from keyboard KeyListener
ItemEvent generated when check-box or list item is clicked ItemListener
TextEvent generated when value of textarea or textfield is changed TextListener
MouseWheelEvent generated when mouse wheel is moved MouseWheelListener
generated when window is activated, deactivated, deiconified,
WindowEvent WindowListener
iconified, opened or closed
generated when component is hidden, moved, resized or set
ComponentEvent ComponentEventListener
visible
ContainerEvent generated when component is added or removed from container ContainerListener
AdjustmentEvent generated when scroll bar is manipulated AdjustmentListener
FocusEvent generated when component gains or loses keyboard focus FocusListener
Steps to handle events:
1. Implement appropriate interface in the class.
2. Register the component with the listener.
Java Event Handling Code
We can put the event handling code into one of the following places:
1. Within class
2. Other class
3. Anonymous class
Java event handling by implementing ActionListener
1. import [Link].*;
2. import [Link].*;
3. class AEvent extends Frame implements ActionListener{
4. TextField tf;
5. AEvent(){
6.
7. //create components
8. tf=new TextField();
9. [Link](60,50,170,20);
10. Button b=new Button("click me");
11. [Link](100,120,80,30);
12.
13. //register listener
14. [Link](this);//passing current instance
15.
16. //add components and set size, layout and visibility
17. add(b);add(tf);
18. setSize(300,300);
19. setLayout(null);
20. setVisible(true);
21. }
22. public void actionPerformed(ActionEvent e){
23. [Link]("Welcome");
24. }
25. public static void main(String args[]){
26. new AEvent();
27. }
28. }
public void setBounds(int xaxis, int yaxis, int width, int height); have been used in the above example that sets
the position of the component it may be button, textfield etc.