30
Oct
a List. Planning Your Event-Handling Strategy A good
a List. Planning Your Event-Handling Strategy A good thing to do before you start writing code is to think about what events you are going to have to handle. Before we can do that, we have to know how exactly an event works. Whenever a Component generates an event, it is sent up the hierarchy until it is either handled or it runs out of Components. For instance, when one of our Buttons is pushed, it generates an Event. This Event is first sent to the Button, then to the Panel in which the Button sits, and then to the Panel in which the first Panel is embedded (which, in this case, would be our Applet). Each Event has certain properties you should be aware of. Basic Events and Their Properties Here is a summary of some of the basic ones we will need (assume we are dealing with an Event object named evt): evt.id is a number that signifies what the Event means. You don t have to memorize id numbers, luckily, because the Event class provides a nice long list of constants that you can use. For instance, whenever an item in a List is selected, a LIST_SELECT Event is generated (evt.id == Event.LIST_SELECT). The table below lists all of the Event.id constants. evt.target is the Object that generated the Event. This is most useful along with the instanceof operator, which is used to determine if a given Object is a subclass of a particular class. For instance, if evt is a LIST_SELECT Event, then (evt instanceof List) will evaluate to true. evt.arg is the argument passed along with the Event. What exactly this is varies from component to component, but Buttons pass a String representing their labels. This is useful for determining which Button has been pressed by the user. These are the major properties of the Event class that we will use. You will need this information to write this applet s handleEvent() method, but we will get to that later. Just be sure you understand the function and operation of Events before continuing. (Sun s tutorial has especially good examples of how this works.) See Table 9-1 for a list of Event.id constants. (For a more extensive discussion of events and event handling in Java, see Chapter 4, Adding Interactivity.) Previous Table of Contents Next
Note: If you are looking for good and quality webspace to host and run your java application check Actions java hosting services
