Professional Documents
Culture Documents
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
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
Let us now see how each of these types of events are handled by Java 1.1(and 1.2) Event model.
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: