Event Handling The Delegation Event Model A source generates an event and sends it to one or more listeners. In this scheme, the listener simply waits until it receives an event. Once received, the listener processes the event and then returns. The advantage of this design is that the application logic that processes events is cleanly separated from the user interface logic that generates those events. Event Sources A source is an object that generates an event. This occurs when the internal state of that object changes in some way Event Listeners A listener is an object that is notified when an event occurs. It has two major requirements. First, it must have been registered with one or more sources to receive notifications about specific types of events using addTypeListener(). Second, it must implement listener methods to receive and process these notifications. Package: java.awt.util.*; and java.awt.event.*; Superclass of all event class: EventObject Methods: 1.Object getSource()- returns source object. 2. String toString() Class Hierarchy in java event
Listener Interface Event Class Source & Methods generated when button is pressed, menu-item is ActionEvent ActionListener selected, list-item is double getActionCommand() clicked,JRadioButton is void actionPerformed(ActionEvent a) selected MouseListener mouseClicked(MouseEvent m) MouseEvent mousePressed(MouseEvent m) Methods: generated when mouse is 1.getPoint()-returns x dragged, moved, clicked, and y co-ordinates of pressed or released also mouse when the enters or exit a component 2.getX() mouseReleased(MouseEvent m) mouseEntered(MouseEvent m) mouseExited(MouseEvent m) MouseDraggedListener 3.getY() mouseDragged(MouseEvent m) mouseMoved(MouseEvent m) KeyListener KeyEvent Methods: 1.getKeyChar() generated when input is received from keyboard 2.getKeyCode() void keyPressed(KeyEvent ke) void keyReleased(KeyEvent ke) void keyTyped(KeyEvent ke) ItemEvent generated when Jcombobox, choice, check- ItemListener box , checkboxmenuitem or void itemStateChanged(ItemEvent ie) list item is single clicked TextEvent generated when value of TextListener
textarea or textfield is changed void textChanged(TextEvent te) WindowListener windowActivated(WindowEvent w) windowClosed(WindowEvent we) WindowEvent generated when window is activated, deactivated, deiconified, iconified, opened or closed windowClosing(WindowEvent we) windowDeactivated(WindowEvent e) windowDeiconified(WindowEvent we) windowIconified(WindowEvent we) windowOpened(WindowEvent we) ComponentEvent ComponentListener generated when component componentResized(ComponentEvent ce) is hidden, moved, resized or componentMoved(ComponentEvent ce) componentShown(ComponentEvent ce) set visible componentHidden(ComponentEvent ce) ContainerEvent ContainerListener generated when component componentAdded(ContainerEvent ce) is added or removed from componentRemoved(ContainerEvent container ce) AdjustmentListener AdjustmentEvent generated when scroll bar is adjustmentValueChanged(AdjustmentEvent manipulated a) FocusEvent generated when component FocusListener void focusGained(FocusEvent fe) gains or loses keyboard focus void focusLost(FocusEvent fe) Adapter Classes • • • Adapter class can simplify the creation of event handlers in certain situations. An adapter class provides an empty implementation of all methods in an event listener interface. Adapter classes are useful when you want to receive and process only some of the events that are handled by a particular event listener interface. Adapter Classe:1.WindowAdapter 2.ComponentAdapter
3.FocusAdapter 4.ContainerAdapter 5.MouseAdapter 6.KeyAdapter The Inner classes Inner class is a class defined within other class, or even within an expression. Annonymous classes: Classes which are defined where their object is initialised is called as anonymous classes. They make the coding concise.