This document was written by CS 290W TA Joshua Kay and was last modified
Let's start talking a little bit more about Events. Basically, an Event takes place anytime something happens to the Applet. It's an action-reaction paradigm. If someone hits you in the face, your face will move, and you will probably retaliate.It's the same thing with an Applet. If we click a button, touch a key, MouseOver on an image - all of these things are events. Now, the difference between real life and programming is that we have to program what happens when the Event occurs.
We already looked at the syntax a little bit earlier, but let's check it out again...
public boolean action(Event e, Object o);
What is this? Well, an Action causes an Event to occur - so action is
the built-in name for the Event method. It is public because the
Button is accessible from all parts of the class, and boolean because
it returns a true/false value.
When an action causes an Event - two parameters are passed to this method - the Event that occurs, and the Object which was the focus of the Event.
Note that the e and o are merely variables set up to hold the Event and Object - it could have been written
public boolean action(Event tom, Object jerry)
had you really wanted.
So now we can check out these Events and Objects. There are two easy ways to check out how to see what Object was affected
The first way you have already seen, and is listed above
In this example, if the Scrollbar is touched, the action sets off an Event and calls our action method - but nothing happens! Because we did not program anything to happen, we leave that as a useless GUI component.
So that takes care (for the most part) of Events occuring to GUI objects. What else do we have?
The (key==8)
comes from the
ASCII code table. critString.length()
returns the length of
the character string. critString.charAt(i)
returns
the character at location i in the character string.
If Criteria is equal to "Please" - then after the Backspace key is pressed, it will equal "Pleas". Check It out...
So that's still simple, right? What about Events that just happen,and not necessarily to Anything? Well, then we have one more public boolean function...
Now that's not too difficult, right? Notice two pretty important things here. When you Move the mouse into the window, Nothing seems to happen - But it actually does. You see, as soon as the Mouse moves into the window, the mouse also moves inside of the window, and so The 'MOUSE_MOVE' is called!
Also - notice that when you move the mouse into the Button - it seems to think that the Mouse is no longer pointing at the Applet window - well, it is, but it is pointing at a GUI component - one more thing to keep track of as you Program Events in JAVA!
Now, Everything here is strictly JDK 1.02 Event-Handling. We are using, remember, the JDK1.1.6, right? Well, when you compile code with this event-handling, you are going to get a warning message that says
We are limited to using the 1.02 JDK Event-Handling, because this is a WebSite course, and therefore we need to be able to see our work on the web. Netscape 4.05, which is the PUCC default browser, does not support JDK1.1 Event-Handling, so we need to go to 1.02.
Don't fret, don't worry... Event handling with the JDK1.1.6 is actually a whole lot simpler, and it makes more sense. it is also more powerful. But the 1.02 Event-handling is supported by all of the 3.x and 4.x browsers, and will still be supported (most probably) in the 5.x generation as well.