The Java programming language uses events to enable GUI form behavior. Source
objects can trigger events which one or more objects with event listeners react
to by means of event handlers.
Defining Event Handlers
You can define event handlers using a component's property sheet or contextual
menu. You can also define an event handler using the Connection wizard.
To define an event handler using the property sheet:
Select the component in the Inspector window.
Click the Events button at the top of the Properties window.
Click the value of the desired event in the list. Initially, the value
for all events is <none>. When you click the value field, <none> is
automatically replaced with the default event name.
Open the Handlers dialog box by clicking
the event's ellipsis (...) button.
Enter the event handler name by clicking the Add button to add a new name
to the list of handlers. Click OK.
The code for the listener and the
empty body of the handler method is generated.
Note that you still must add the desired
code for the new event handler in the Source Editor.
To define an event handler using the contextual menu:
Right-click a form component in the Files window, Project window, or
Inspector window.
Choose Events from the contextual menu and its submenus. Bold menu items
in the Events submenus indicate event handlers that have already been defined.
The code for the listener and the empty body of the handler method is
generated. The default name is assigned to the event handler.
Add your code for the new event handler in the Source Editor.
Notes:
If multiple events are of the same type, you can use the same
handler for all of them. For example, you could set both focusGained and focusLost to
use the button1FocusChange handler since they are
both of the type java.awt.event.FocusEvent. You can also use the same handler
for the same event on multiple components.
You can set the style of how the code for
component events and their handlers is generated. Choose Tools > Options.
Select the Miscellaneous pane and then select the GUI Builder tab. Set the Listener
Generation Style property. You can choose between the Anonymous Innerclasses,
One Innerclass, and Main Class styles.
Adding Multiple Handlers for One Event
In the Inspector window, select the component for which you want
to add multiple handlers.
Click the Events button at the top of the Properties window.
Select the event in the property sheet and click the ellipsis (...) button
to display the Handlers dialog box. Repeat these steps to add additional
event handlers.
Removing Event Handlers
In the Inspector window, select the component whose event handler you
want to remove.
Click the Events button at the top of the Properties window.
Select the event in
the property sheet and click the ellipsis (...) button to display the Handlers
dialog box. Alternately, you can simply delete the name of the handler you
want to remove in the Properties window.
In the Handlers dialog box, select the handler to remove
from the list and click Remove.
When you remove an event handler, the corresponding code block is also deleted.
If more than one handler uses the same name and same block of code, deleting
a single reference to the code does not delete the code itself. Only deleting
all references will delete the corresponding code block, and a confirmation
dialog box is displayed first.
For more information about how to handle events in your program, including
information about the Java event model, see the Creating a GUI with JFC/Swing tutorial
at: