HANDLING EVENTS


In order to make the GUI for the Hilo game useful, we need to specify what actions are to be performed whenever the DEAL button is clicked and when text is typed into the "Your Bet:" text field. An action such as a mouse click or a carriage return (enter) in a text field causes an event to occur in a Java program.

Click here on hilo game if you wish to review the Hilo game and its GUI. You can also review the source code that we wrote previously for the hilo_win class, which created the GUI for the Hilo game.

Events

An event is an instance of class Event, which is in the AWT library. There are many public instance variables that provide information about an event, and there are also many public methods that are useful in programs that generate or manipulate events. For our purposes here the only component of an event object that we will use is the public instance variable target, whose value is the component (e.g., widget object) with which the event is associated. Further information on the Event class can be found in Java's Event API documentation.

Handling the Events for the Hilo GUI

To complete the Hilo GUI, we want to act on the events created by a player of the Hilo game. In order to do this, we must identify what events to handle and what to do for those events:

The action() Method

In Java's AWT, as in most windowing systems, whenever a button is pressed, whenever the mouse is clicked, or whenever another event occurs in a window, there must be an action defined for this event in order for the program to do anything useful. The Java window manager handles an event by passing it to the window's action() method. When the class hilo_win inherits from the Applet class, it also inherits an action() method. This inherited method does nothing of real use, so we must override this method with our own action() method.

The action() method for an applet must be declared public and must return a boolean value as to whether or not an action has occurred (i.e., whether the event has been handled). The method returns true to indicate that it has successfully handled the action or false if the event that triggered the action should be passed up to the component's (widget's) parent. The action() method receives two parameters: an Event instance and an Object instance. The Event parameter is the event that occurred, while the Object parameter can provide additional information such as the label (name) of a button that was pressed. Rather than further general discussion, let's look at the action() method for our Hilo applet:


1: public boolean action (Event evt, Object arg) { 2: /* Deal with the actions of the window */ 3: // Deal button hit 4: if (evt.target instanceof Button) { 5: if (evt.target == deal_button) { 6: deal_button.disable(); 7: deal(); 8: return true; 9: } 10: return false; 11: } 12: // Text field had data entered into it 13: else if (evt.target instanceof TextField) { 14: if (evt.target == bet_text) { 15: bet_text.disable(); 16: bet(); 17: return true; 18: } 19: return false; 20: } 21: return false; 22: }

Looking at the Code

Let's first take a look at lines 4 and 13 of the action() method. These lines contain the if statements to determine what type of component was the target of the action by using the instanceof operator to test whether an object is an instance of a specific class. Line 4, if (evt.target instanceof Button) {, checks to see if the target of the event was a Button component. Line 13 checks to see if the target of the event was a TextField. (Textfield events are created when the user presses the return (enter) key in an editable text field window, and not when characters are typed into the field.)

In our program we have only one possible button that could be pressed, but let's still look at how to determine which one was pressed when we have a program with more than one button. A way to do this is illustrated in line 5: if (evt.target == deal_button) {. (This test could be omitted in our Hilo applet, although some would argue that it is good programming practice to include it in case other buttons are added later.)

Lines 6-8 are the actions for a click on the Deal buton. The button is first disabled to prevent another click, and the deal() method is applied (to the applet). (The deal() method will be discussed in a later section.)

Now let's take a look at line 14: if (evt.target == bet_text) {. Line 13 determined whether the event occurred on a Textfield object, and in line 14 we determine whether the event occurred on the bet_text text field. In our Hilo GUI window the only text field where an event can occur is on the bet_text text field, because it is the only text field that allows user input. (It is the only text field to which setEditable(false) was not applied.) So this test could be omitted in our hilo_win action() method, but it is included here to illustrate how the determination could be made in a program with more than one editable text field.

The actions for a bet_text event are similar to those for a Deal button event. The text field is disabled to prevent further input, and the bet() method is used to perform the other actions. (The bet() method will be discussed in a later section.)

We are now ready to complete the Hilo applet, and we begin by looking at its design in the next section.