Unformatted text preview:

GUI Event HandlingWhat is an Event?Handling EventsAn example of Event HandlingThe Event Handling processThe Event Handling Process (contd..)What does an Event Handler require?What does an Event Handler require? (contd..)Slide 9Types of EventsTypes of Events (contd..)The Event classesEvent ListenersThe ActionListener MethodThe MouseListener MethodsThe MouseListener Methods (contd..)The MouseMotionListener MethodsThe WindowListener MethodsThe WindowListener Methods (contd..)Slide 20Hierarchy of event objectsAdditional Listener TypesAdapter classes for Event Handling.Adapter classes - an Illustration.Illustration (contd..)Slide 26How to use an Adapter class?Using Inner classes for Event HandlingUsing Inner classes (contd..)Creating GUI applications with Event Handling.Creating GUI applications with Event Handling (contd..)Slide 32Design ConsiderationsSlide 34Common Event-Handling IssuesCommon Event-Handling Issues (contd..)Slide 37ReferencesGUI Event HandlingNithya RamanWhat is an Event?•GUI components communicate with the rest of the applications through events.•The source of an event is the component that causes that event to occur.•The listener of an event is an object that receives the event and processes it appropriately.Handling Events•Every time the user types a character or clicks the mouse, an event occurs.•Any object can be notified of any particular event.•To be notified for an event, The object has to be registered as an event listener on the appropriate event source.The object has to implement the appropriate interface.An example of Event Handlingpublic class SwingApplication implements ActionListener { ... JButton button = new JButton("I'm a Swing button!"); button.addActionListener(this); .... public void actionPerformed(ActionEvent e) { numClicks++; label.setText(labelPrefix + numClicks);}}The Event Handling process•When an event is triggered, the JAVA runtime first determines its source and type.•If a listener for this type of event is registered with the source, an event object is created.•For each listener to this type of an event, the JAVA runtime invokes the appropriate event handling method to the listener and passes the event object as the parameter.The Event Handling Process(contd..)What does an Event Handler require?•It just looks for 3 pieces of code!•First, in the declaration of the event handler class, one line of code must specify that the class implements either a listener interface or extends a class that implements a listener interface.public class DemoClass implements ActionListener {What does an Event Handler require? (contd..)•Second, it looks for a line of code which registers an instance of the event handler class as a listener of one or more components because, as mentioned earlier, the object must be registered as an event listener.anyComponent.addActionListener(instanceOf DemoClass);What does an Event Handler require? (contd..)•Third, the event handler must have a piece of code that implements the methods in the listener interface. public void actionPerformed(ActionEvent e) { ...//code that reacts to the action... }Types of Events•Below, are some of the many kinds of events, swing components generate.Act causing Event Listener TypeUser clicks a button, presses Enter, typing in text fieldActionListenerUser closes a frame WindowListenerClicking a mouse button, while the cursor is over a componentMouseListenerTypes of Events (contd..)Act causing Event Listener TypeUser moving the mouse over a componentMouseMotionListenerComponent becomes visible ComponentListenerTable or list selection changes ListSelectionListenerThe Event classes•An event object has an event class as its reference data type.•The Event object classDefined in the java.util package.•The AWT Event class An immediate subclass of EventObject.Defined in java.awt package.Root of all AWT based events.Event Listeners•Event listeners are the classes that implement the <type>Listener interfaces.Example:1. ActionListener receives action events2. MouseListener receives mouse events. The following slides give you a brief overview on some of the listener types.The ActionListener Method•It contains exactly one method.Example:public void actionPerformed(ActionEvent e)The above code contains the handler for the ActionEvent e that occurred.The MouseListener Methods•Event handling when the mouse is clicked.public void mouseClicked(MouseEvent e)•Event handling when the mouse enters a component.public void mouseEntered(MouseEvent e)•Event handling when the mouse exits a component.public void mouseExited(MouseEvent e)The MouseListener Methods (contd..)•Event handling when the mouse button is pressed on a component.public void mousePressed(MouseEvent e)•Event handling when the mouse button is released on a component.public void mouseReleased(MouseEvent e)The MouseMotionListener Methods •Invoked when the mouse button is pressed over a component and dragged. Called several times as the mouse is draggedpublic void mouseDragged(MouseEvent e)•Invoked when the mouse cursor has been moved onto a component but no buttons have been pushed. public void mouseMoved(MouseEvent e)The WindowListener Methods •Invoked when the window object is opened.public void windowOpened(WindowEvent e)•Invoked when the user attempts to close the window object from the object’s system menu.public void windowClosing(WindowEvent e)The WindowListener Methods (contd..)•Invoked when the window object is closed as a result of calling dispose (release of resources used by the source).public void windowClosed(WindowEvent e)•Invoked when the window is set to be the active window.public void windowActivated(WindowEvent e)The WindowListener Methods (contd..)•Invoked when the window object is no longer the active windowpublic void windowDeactivated(WindowEvent e)•Invoked when the window is minimized.public void windowIconified(WindowEvent e)•Invoked when the window is changed from the minimized state to the normal state.public void windowDeconified(WindowEvent e)Hierarchy of event objectsNote: The number of event objects is much greater then specified in diagram…Due to space constraints, only some of them are represented in the figureCourtesy: Safari.oreilly.comAdditional Listener Types•Change Listener •Container Listener•Document Listener•Focus Listener•Internal Frame Listener•Item Listener•Key Listener•Property Change


View Full Document

NJIT CS 602 - GUI Event Handling

Download GUI Event Handling
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view GUI Event Handling and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view GUI Event Handling 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?