<HTML>
<HEAD>

<!-- Change the Title please -->
<TITLE>AWT SHORT COURSE: Events</TITLE>
</HEAD>

<!-- BODY-TEMPLATE -->

<!-- NAVBAR-PULLQUOTE-TEMPLATE -->
<TR VALIGN=TOP>
<td width=178>

<BR clear=LEFT>
<TABLE ALIGN=LEFT>
<TR VALIGN=TOP>
<td width=60>
<a href="../TOC.html"><img border=0 src="/developer/images/magelang/contents.gif" alt="contents" WIDTH=147 HEIGHT=23></a>
</td>
</tr>
<tr>
<td>
<a href="NestedEvent.html"><img border=0 src="/developer/images/magelang/previous.gif" hspace=10 alt="Previous" width=64 height=20></a>
</td>
<td>
<a href="EventConvenience.html"><img border=0 src="/developer/images/magelang/next.gif" alt="Next" width=43 height=20></a>
</td>
</tr>
</table>

</TR>
</TABLE>

<!-- PAGETOP-TEMPLATE -->
<TR>
<TD WIDTH=452 ALIGN=RIGHT>
<img src=images/ShortCourse.gif>
</TD>
</TR>
<TR>
<TD WIDTH=452 COLSPAN=2 ALIGN=right>
<!--breadcrumb trail; change to reflect the current URL-->
<!-- Please change to reflect where _exactly_ you are in the site
<!-- Add more subcategories as needed  -->

<!-- Please Change these.  The Links are SubCat#, and titled SubCategory# -->
</TD>
</TR>

<!--- Please put everything you want here to the right of the navbar.
	<TR>
	<TD>
	This is the MAIN TABLE.  Put data HERE unless you need a table that
 	REQUIRES the full screen width, i.e. an event schedule table.
	Add a new table row for the data.

	If the text needs to be in two columns, use this <TR> structure:
	Make the width=452
	<TR>
	<TD WIDTH=226>
	</TD>
	<TD WIDTH=226>
	</TD>
	</TR>

	</TD>
	</TR>
--->

<TR>
<TD width=452>
<p>
<a name="Events">
<font size=+2>Events</font><P>
</a>

Once an applet has constructed a graphical interface, it waits for an <em>event</em> such as a mouse click or a Button press to trigger an action.  All Component objects except Label can be triggered into action by an event.  An event is sent to a component by calling its handleEvent method with an <a href="http://java.sun.com:80/products/JDK/CurrentRelease/api/java.awt.Event.html">Event</a> object as an argument, which contains information about the activated object and the type of event.  Method handleEvent, in turn, interprets the type of event and calls one of several <em>convenience</em> methods such as mouseDown and keyDown.  Consequently, to have a Component respond to an event, all you have to do is override the right method in the appropriate subclass of Component.

<p>
When triggered, a Component can either respond to the event itself or let the event be handled by the enclosing Container object.  If the enclosing Container does not handle the event, its enclosing Container is sent the event until the top of the containment hierarchy is reached, or a Component handles the event.  Components indicate that they have handled an event by returning true from handleEvent or from one of the convenience methods.  Returning false causes the event to move up a level in the containment hierarchy.  The path of Component objects taken by an event is called the <em>event chain</em>.
<p>
Note: This applies to the Event model in the JDK 1.0.2. The JDK 1.1 will be backwards compatible, but the Event model will be different.

<p>
We begin with a simple example of how an applet responds to a mouse event.  Consider the following applet that prints "hi" everywhere you click the mouse:

<p>
<center>
<hr>
<applet code=SimpleEvent.class CODEBASE="/applets/magelang/AWT-Training/Examples/" width=200 height=100></applet>
<hr>
</center>

<p>
Corresponding source code:

<p>
<pre>
import java.awt.*;
import java.applet.Applet;

public class SimpleEvent extends Applet {
    public boolean mouseDown(Event e,int x,int y) {
        // upon mouse click, put "hi" at that location
        // since graphics context is not passed in,
        // we obtain it via getGraphics() method.
        getGraphics().drawString("hi", x, y);
        return true; // say that we handled this event
    }
}
</pre>

<p>
Because Container objects such as Panel and Applet are also subclasses of Component, they too may respond to events.  We have trapped the mouseDown event in the Applet subclass simply by overriding method mouseDown.  Had we not overridden this method, Component.mouseDown would have been called, which does nothing but return false indicating that it did not handle the event.  There are a number of other events that you may want to handle in your applet.  See <a href="EventConvenience.html">Event Convenience Methods</a>.

<p>
Clicking the mouse over a non-Container Component does not normally generate a mouseDown event in the current JDK implementation.  A mouse click normally activates a Component that triggers a call to method action.  For example, pushing a Button invokes method action up the event chain until some object handles the event (returns true).  The action method is passed the Event object as well as the arg field of that Event.  For a Button, the argument is the String label of that Button.  In <a href="ActionEvents.html">Action Events</a> we chronicle the event arguments for the various components.

<p>
This section includes the following subsections:

<ul>
<li><a href="EventConvenience.html">Event Convenience Methods</font>
<li><a href="ActionEvents.html">Action Events</a>
<li><a href="EncapsulatingBehavior.html">Encapsulating Behavior in Components</a>
</ul>
</TD>
</TR>
</TABLE>

<!--
        This table is for everything that goes under the 
        navbar, it is a full screen table. Use this table 
        ONLY for full screen width tables like an event 
        hourly schedule.
-->

<br clear=left>
<TABLE ALIGN=left>

<tr>
<td WIDTH=600 align=right>

<!--Last Modified and copyright message

    Please modify the date whenever significant changes are made to 
    a page; leave the copyright alone
-->

</td>
</tr>
</TABLE>

</BODY>
</HTML>
