Events, (Camera), Action!

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...

import java.awt.*; import java.applet.*; public class EventTest extends Applet { Button testButton=new Button("test"); Label testLabel=new Label("test label"); public void init() { add(testButton); add(testLabel); } public boolean action(Event e, Object o) { if (e.target==testButton) { testLabel.setText("Yo! test works!"); return true; } else return false; } } This is all pretty easy. But let's look at it anyway.

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

if (e.target==testButton) { testLabel.setText("Yo! test works!"); } Now this is pretty cool to check between different Buttons, but what if you only have one Button, like here? Or what if you want to discriminate between Buttons and Scrollbars? Check this out... if (e.target instanceof Button) { testLabel.setText("Yo! test works!"); } So let's look at some more code. We'll add a Scrollbar here, okay? import java.awt.*; import java.applet.*; public class EventTest extends Applet { Button testButton=new Button("test"); Scrollbar scrollTest=new Scrollbar(); Label testLabel=new Label("test label"); public void init() { add(testButton); add(scrollTest); add(testLabel); } public boolean action(Event e, Object o) { if (e.target instance of Button) { Label.setText("Yo! test works!"); return true; } else return false; } } Now, we have a program that only checks to see whether a Button is pressed. This can be useful many times, especially if you have multiple buttons. Think about how many times you see the phrase 'Press any key to continue'...

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?

TextField criteria = new TextField(25); public boolean keyDown(Event e, int key) { if(key==8) { String temp=""; String temp2=""; if(criteria.getText()!="") { String critString=criteria.getText(); for (int i=0; i<(critString.length())-1; i++) { temp2=(""+critString.charAt(i)); temp=temp.concat(temp2); } criteria.setText(temp); } } } This introduces a few methods we haven't seen, but it is a good example of keyDown(). Again, it is a public and boolean event, just like with the GUI components. The code above checks for the Backspace key, and when pressed, shrinks the String Criteria by one character.

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...

import java.awt.*; import java.applet.*; public class EventTest extends Applet { Button testButton=new Button("test"); Label labelTest= new Label("test label is really long la lallallaalalallala"); public void init() { setBackground(Color.blue); add(testButton); add(labelTest); } public boolean handleEvent(Event e) { if(e.id==Event.MOUSE_MOVE) { labelTest.setText("The Mouse Moved!"); return true; } else if(e.id==Event.MOUSE_EXIT) { labelTest.setText("The Mouse Moved out of the Applet Window!"); return true; } else if(e.id==Event.MOUSE_ENTER) { labelTest.setForeground(Color.red); labelTest.setText("The Mouse Moved into the Applet Window!"); return true; } else if(e.id==Event.MOUSE_DOWN) { labelTest.setText("The Mouse Button has been Pressed"); return true; } else if(e.id==Event.MOUSE_UP) { labelTest.setText("The Mouse Button has been Released"); return true; } else if(e.id==Event.KEY_PRESS) { labelTest.setText("A key has been pressed!"); return true; } else if(e.id==Event.KEY_RELEASE) { labelTest.setText("A key has been released!"); return true; } else return false; } } Let's take a look...

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

Note: EventTest.java uses a deprecated API. Recompile with "-deprecation" for details. 1 warning. What this means is that these Event-Handling methods have been rewritten in a later release of the JDK, and that although they are still currently supported, they might not be in the next release of the JDK.

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.