Java – Topic 28

(Event Driven Programming)

 

 

LESSON NOTE

 

 

INTRO

           

Event driven programs are programs that run a sequence of code when an event (such as a mouse click or a key press) occurs.

 

To do this in java, we simply need to include what is a called a listener.  A listener is an object that is responsible for watching (listening) for events.  When an event occurs, that object has code to run.

 

INTERFACES

 

Interfaces are simply special classes that have a list of methods without method bodies.  Classes can then implement interfaces.

 

MOUSELISTENER INTERFACE

 

If you look up MouseListener in the java documentation, you will find that it is an interface with the following methods:

 

      void mouseClicked(MouseEvent e)

      void mouseEntered(MouseEvent e)

      void mouseExited (MouseEvent e)

      void mousePressed(MouseEvent e)

      void mouseReleased(MouseEvent e)

           

Therefore, you can create an object that will listen for mouse events.  All it has to do is implement the MouseListener interface.  Inside that class, the five methods above must be implemented.

 

WHO LISTENS?

 

The easiest way to implement the MouseListener is to do so in your Applet class.  Therefore, your code for the class name will now look like this:

 

                        public class AppletName extends Applet implements MouseListener

 


WHAT WILL THIS LOOK LIKE?

 

This might seem a little messy but its not too bad once you start working with it.  The green text shows you the new code you are adding.

 

            public class AppletName extends Applet implements MouseListener

            {

               public void init()

               {

                  …

                  addMouseListener(this);

   }

 

   public void paint(Graphics g)

   {…}

 

         public void mouseClicked(MouseEvent e)

         {…}

 

         public void mouseEntered(MouseEvent e)

         {…}

 

         public void mouseExited (MouseEvent e)

         {…}

 

         public void mousePressed(MouseEvent e)

         {…}

 

         public void mouseReleased(MouseEvent e)

         {…}

}

 

SHOWSTATUS METHOD

 

The applet class contains a method called showStatus(String msg) that will display the msg in the status bar at the bottom of your browser.  This is convenient at many time including when testing your applets.

 

MOUSE EVENT

 

You probably noticed that every method in the MouseListener interface has a MouseEvent argument.  This is simply an object that is automatically created that contains information about the event. 

 

The methods you will most likely use from this object are getX() and getY() which return the x and y coordinates of the event.

 

EXAMPLE 1

 

Click here to see the applet.  Click here to see the code.  Make sure your understand it!

 

You can also click here to download all the applet files.

 

EXAMPLE 2

 

The following applet will simply show the coordinates of the mouse click.  The code also shows you how to deal with events that you don’t care about.

 

Click here to see the applet.  Click here to see the code. 

 

You can also click here to download all the applet files.