Basic Programming using Java
Event Handling in AWT
Event Handling in AWT
At the end of this chapter you will be able to understand various types of events associated with AWT components and how to handle them.
SCOPE
8.1 Event Handling in Java 8.2 Handling Mouse Clicks 8.3 Handling Mouse Movements 8.4 Handling Keyboard Events 8.5 Handling Component Events 8.5.1 Handling Buttons 8.5.2 Handling Checkboxes and Radio buttons 8.5.3 Handling Choices and Lists 8.5.4 Handling Scrollbars 8.5.5 Handling Windows, Frames and Dialog boxes 8.5.5.1 Frame 8.5.5.2 Dialog 8.5.6 Case Study College Student Enrolment System (Contd.) 8.5.7 Handling Menus
Basic Programming using Java
Event Handling in AWT
8.1 Event Handling in Java
In Chapter 7, an introduction to creating AWT components was given. With the help of AWT you can create components like Buttons, Labels, Checkboxes, Radio buttons, etc. But it is equally important to handle the user response to these components. For example, if a checkbox has been checked, the program should be able to record this information. In this case clicking of the checkbox is an event. An event is generated in response to anything that a user does, when a program is running. For example, the movement of mouse, button click, a key press - all of these activities generate different types of events. Events can be broadly categorized into four categories: Mouse Click Event mouse is pressed down or released or is clicked (pressed and released both at the same location) Mouse Movements mouse is moved or dragged (pressed down and moved) Keypresses a key is pressed, released, or typed (pressed and released both) User-interface events a button is clicked, checkbox is checked, scroll bar is moved up and down Each type of event is handled differently by Java 1.1 and Java 1.2 Event model. Java 1.1 and 1.2 event model is very different from Java 1.0 event model. In Java 1.0 Event model, an object that needs to handle events has to be a sub-class of Component class. This restriction is not there in Java 1.1 and 1.2 Event model. Here any class can receive and manage events even if it is not a sub-class of Component class. This is done because many times an event is handled by some other object. For example a button click might trigger an action in an object other than the button. For this purpose Java 1.1 and 1.2 Event model support the Event Listeners. This event model is also called as Delegation Event Model as it allows you to designate any object to be an event listener. You can completely control how events are transmitted from the event sources (such as buttons, lists) to event listeners. An Event Listener is any object that implements one or more listener interfaces. There are different listeners for each category of AWT components. For example, MouseListener will be able to receive mouse clicks and MouseMotionListener will be able to receive mouse movements. Similarly there are other listeners for many other AWT components. Event sources have methods that allow you to register event listeners with them. When an event happens to the source, the source sends the notification of that event to all the listener objects that are registered for that event. The information about the event is encapsulated in the event object (all event objects ultimately derive from the class java.util.EventObject). Basic steps for event handling in Java 1.2 model can be given as: Listener object is an instance of a class that implements a listener interface, which includes methods to be called when an event is triggered. The eleven listener interfaces are: ActionListener KeyListener MouseListener AdjustmentListener ComponentListener MouseMotionListener ContainerListener TextListener
Basic Programming using Java
Event Handling in AWT
For example,
Class UPanel extends Panel implements ActionListener{ public void actionPerformed(ActionEvent e){ } }
An event source is an object that can register listener objects and send them event objects. To register event listener object with event source the following syntax is used
eventSourceObj.addEventListener(eventListenerObj); For example, UPanel panel1 = new UPanel(); Button button1 = new Button(Ok); button1.addActionListener(panel1);
Statement button1.addActionListener(panel1), registers the event actionlistener object panel1, with event source button1. When the event occurs event source sends out event objects to all registered listeners. For example, When the user presses Ok button an ActionEvent object is created and panel1.actionPerformed is called with ActionEvent object as a parameter. The listener objects then use the information in the event object to respond to the event.
UPanel ActionListener Interface actionPerformed() 1 UPanel object is passed to Ok buttons addActionListener () method
2 Button press causes actionPerformed() of listeners to be called
addActionListener () Fig 8.1 Steps in Java 1.2 Event Model
Let us now see how each of these types of events are handled by Java 1.1(and 1.2) Event model.
8.2 Handling Mouse Clicks
Basic Programming using Java
Event Handling in AWT
Mouse click is one of the most common events since most of the environments today are GUI environments. One can use mouse clicks to do a variety of things like clearing the screen, displaying a shortcut menu or help, turning sound on or off, etc. The MouseListener interface can be used to handle mouse clicks. In case an applet wants to tap mouse click events, it must implement MouseListener interface. This interface has five methods, all of which must be overridden by the applet. These methods are: