Many elements in a DHTML program can fire events. A button is the obvious example. For example, one of the actions you'd probably like to know about in your code is when the person viewing a page clicks a button. And you'll also want to know which button was clicked. Here's how:
DhButton myButton = new DhButton();
.
.
.
document.add(myButton);
myButton.addOnClick(new DhEventHandler(this.myButtonClick));
.
.
.
void myButtonClick(Object sender, DhEvent e)
{
myButton.setText("I've been clicked");
}
This code says that, for the button called myButton, call myButtonClick whenever the button fires the OnClick event (that is, when it is clicked). The code inside the myButtonClick() event handler does very little in this example. It just sets the caption on the button to some new text.
Events are fired all the way up a containment tree; this means that the OnClick event can equally be seen by the button's container as by the button itself. Although typically programmers handle events in the container closest to the event, this event bubbling model can be useful in special cases, giving the programmer the flexibility to decide the best place to code the event handlers.
Many different events can be fired by elements in DHTML, and you can catch them all in the same way. For example, to catch the OnMouseOver event on a button:
DhButton myButton = new DhButton();
.
.
.
document.add(myButton);
myButton.addOnMouseOver(new DhEventHandler(this.myButtonMouseOver));
.
.
.
void myButtonMouseOver(Object sender, DhEvent e)
{
myButton.setText("I can feel that mouse");
}
All the events that can be fired (and caught) are defined in the DhEvents class.