1 / 15

The Event Class

Java GUI’s are event driven , meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse button, hitting ‘Enter’ in a text field, clicking a button …. you get the idea

prentiss
Download Presentation

The Event Class

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse button, hitting ‘Enter’ in a text field, clicking a button …. you get the idea When an event occurs, an event object is created which stores information about that event. This event object may be utilized in the case that the program wishes to respond to the event.

  2. The Event Class The class from which the event object is created depends upon the type of event which has occurred. For example: An object is created using the MouseEvent class if the event involved the mouse. An object is created using the ActionEvent class if a button is pressed, or textfield data has been entered. *these event classes are provided by java.awt.event package.

  3. The Event Source The event source is the visual component with which the user has interacted . The event source creates the event object when an event has occurred. For example: an applet is the event source of a mouse click a button is the event source of a button click

  4. The Event Listener An event listener is an object that is prepared to ‘handle’ the event, if it is notified that the event has occurred. The event listener object receives an event object when it is notified of the event, and responds to the event using the information provided in the event object.

  5. How does it work? Each Java event source keeps an internal list of listeners . If an event occurs, the event source notifies all the listener objects on the list. Any listener that is ‘listening’ for this particular event may then respond. What does the programmer do to make a GUI respond to events? The programmer must: * provide a class from which a listener object can be created (a listener class) * create and register a listener object with an event source

  6. Creating a ‘Listener’ class A class which is to be used for creating a mouse listener object must implement the MouseListener interface. *also in java.awt.event package public interface MouseListener{void mousePressed(MouseEvent event); //Called when a mouse button has been pressed void mouseReleased(MouseEvent event);  //Called when a mouse button has been released void mouseClicked(MouseEvent event); //Called when the mouse has been clicked void mouseEntered(MouseEvent event); //Called when the mouse enters a component void mouseExited(MouseEvent event); //Called when the mouse exits a component }

  7. MouseSpy.java import java.awt.event.MouseEvent; import java.awt.event.MouseListener; // This listener simply prints out the listener method name // and mouse position for each mouse event public class MouseSpy implements MouseListener { public void mousePressed(MouseEvent event) { System.out.println("Mouse pressed. x = " + event.getX() + " y = " + event.getY()); } public void mouseReleased(MouseEvent event) { System.out.println("Mouse released. x = " + event.getX() + " y = " + event.getY()); }

  8. public void mouseClicked(MouseEvent event) { System.out.println("Mouse clicked. x = " + event.getX() + " y = " + event.getY()); } public void mouseEntered(MouseEvent event) { System.out.println("Mouse entered. x = " + event.getX() + " y = " + event.getY()); } public void mouseExited(MouseEvent event) { System.out.println("Mouse exited. x = " + event.getX() + " y = " + event.getY()); } }

  9. Creating a Listener Object and ‘Registering’ It * a listener object is created just like any other – by calling a class constructor. * all event sources are required to provide methods for ‘registering’ event listener objects. We must just call that method.

  10. MouseSpyApplet.java import java.applet.Applet; // This applet installs a mouse spy, which responds to the // five mouse events. public class MouseSpyApplet extends Applet { public MouseSpyApplet() { MouseSpy listener = new MouseSpy(); addMouseListener(listener); //adds object listener to list of registered // event listeners for this event source } }

  11. A mouse listener class does not need to be in it’s own file … it can be a private class . public class MouseSpyApplet extends Applet { public MouseSpyApplet() { MouseSpy listener = new MouseSpy(); addMouseListener(listener); } private class MouseSpy implements MouseListener { public void mousePressed(MouseEvent event) { //code } public void mouseReleased(MouseEvent event) { //code } public void mouseClicked(MouseEvent event) { } public void mouseEntered(MouseEvent event) { } public void mouseExited(MouseEvent event) { } } }

  12. A mouse listener class does not need to be in it’s own file … it can be an inner class. public class MouseSpyApplet extends Applet { public MouseSpyApplet() { class MouseSpy implements MouseListener { public void mousePressed(MouseEvent event) { //code } public void mouseReleased(MouseEvent event) { //code } public void mouseClicked(MouseEvent event) { } public void mouseEntered(MouseEvent event) { } public void mouseExited(MouseEvent event) { } } MouseSpy listener = new MouseSpy(); addMouseListener(listener); } }

  13. The constructor can still perform other tasks as well. public class AnApplet extends Applet { public AnApplet() { // initialize instance variables ********the code********** // get input from user *************the code ********** class MouseSpy implements MouseListener { // listener methods } MouseSpy listener = new MouseSpy(); addMouseListener(listener); } public void paint (Graphics g) { ******* the code************** } }

  14. Code an applet which draws a rectangle. When the mouse is clicked the rectangle should be redrawn at the mouse position. (MoveRec.java on website) Code an applet which displays two rectangles, one blue and one red, and also draws a blue ellipse. When the mouse clicks on either of the two rectangles, the rectangle color should change to the same color as the box. (ColorEllipse.java on website) The MouseAdapter class can make coding the listener classes less tedious by eliminating the need for empty methods. Just extend MouseAdapter rather than implementing MouseListener. Since a MouseAdapter implements MouseListener, any class that extends it also implements MouseListener !!

  15. Applets can also ‘listen’ for any mouse motion. The listener object that does this implements the MouseMotionListener interface. Check it out, and code an applet which draws a filled square, such that when the mouse is placed on the square and the button clicked, the mouse drags the square with it. (MouseDrag.java on website)

More Related