Guidelines for Creating Applications Handling GUI Events Mouse Events Example

J.E.D.I.

8.4.3 MouseMotionListener Methods

The MouseMotionListener has two methods to be implemented. MouseListener Methods public void mouseDraggedMouseEvent e Contains the code for handling the case wherein the mouse button is pressed on a component and dragged. Called several times as the mouse is dragged. public void mouseMovedMouseEvent e Contains the code for handling the case wherein the mouse cursor is moved onto a component, without the mouse button being pressed. Called multiple times as the mouse is moved. Table 27: The MouseMotionListener methods

8.4.4 WindowListener Methods

These are the methods of the WindowListener interface. WindowListener Methods public void windowOpenedWindowEvent e Contains the code for handling the case when the Window object is opened i.e., made visible for the first time. public void windowClosingWindowEvent e Contains the code for handling the case when the user attempts to close Window object from the objects system menu. public void windowClosedWindowEvent e Contains the code for handling the case when the Window object was closed after calling dispose i.e., release of resources used by the source on the object. public void windowActivatedWindowEvent e Invoked when a Window object is the active window i.e., the window in use. public void windowDeactivatedWindowEvent e Invoked when a Window object is no longer the active window. public void windowIconifiedWindowEvent e Called when a Window object is minimized. public void windowDeiconifiedWindowEvent e Called when a Window object reverts from a minimized to a normal state. Table 28: The WindowListener methods

8.4.5 Guidelines for Creating Applications Handling GUI Events

These are the steps you need to remember when creating a GUI application with event handling. Introduction to Programming II Page 110 J.E.D.I. 1. Create a class that describes and displays the appearance of your GUI application. 2. Create a class that implements the appropriate listener interface. This class may refer to the same class as in the first step. 3. In the implementing class, override ALL methods of the appropriate listener interface. Describe in each method how you would like the event to be handled. You may give empty implementations for methods you dont want to handle. 4. Register the listener object, the instantiation of the listener class in step 2, with the source component using the addTypeListener method.

8.4.6 Mouse Events Example

import java.awt.; import java.awt.event.; public class MouseEventsDemo extends Frame implements MouseListener, MouseMotionListener { TextField tf; public MouseEventsDemoString title{ supertitle; tf = new TextField60; addMouseListenerthis; } public void launchFrame { Add components to the frame addtf, BorderLayout.SOUTH; setSize300,300; setVisibletrue; } public void mouseClickedMouseEvent me { String msg = Mouse clicked.; tf.setTextmsg; } public void mouseEnteredMouseEvent me { String msg = Mouse entered component.; tf.setTextmsg; } public void mouseExitedMouseEvent me { String msg = Mouse exited component.; tf.setTextmsg; } public void mousePressedMouseEvent me { String msg = Mouse pressed.; tf.setTextmsg; } public void mouseReleasedMouseEvent me { String msg = Mouse released.; tf.setTextmsg; } public void mouseDraggedMouseEvent me { String msg = Mouse dragged at + me.getX + , + me.getY; tf.setTextmsg; } public void mouseMovedMouseEvent me { String msg = Mouse moved at + me.getX + , + me.getY; tf.setTextmsg; Introduction to Programming II Page 111 J.E.D.I. } public static void mainString args[] { MouseEventsDemo med = new MouseEventsDemoMouse Events Demo; med.launchFrame; } } Here are some screen shots of MouseEventsDemo: Figure 8.2: Running MouseEventsDemo Figure 8.3: Running MouseEventsDemo: Mouse enters the Frame Introduction to Programming II Page 112 J.E.D.I. Figure 8.4: Running MouseEventsDemo: Mouse exits the Frame Introduction to Programming II Page 113 J.E.D.I.

8.4.7 Close Window Example