Managing Component Events

See Also

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:

  1. Select the component in the Inspector window.
  2. Click the Events button at the top of the Properties window.
  3. 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.
  4. Open the Handlers dialog box by clicking the event's ellipsis (...) button.
  5. 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.

To define an event handler using the contextual menu:

  1. Right-click a form component in the Files window, Project window, or Inspector window.
  2. 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.

  3. Add your code for the new event handler in the Source Editor.

Notes:

Adding Multiple Handlers for One Event

  1. In the Inspector window, select the component for which you want to add multiple handlers.
  2. Click the Events button at the top of the Properties window.
  3. 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

  1. In the Inspector window, select the component whose event handler you want to remove.
  2. Click the Events button at the top of the Properties window.
  3. 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.
  4. 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:

See Also
Setting Events With the Connection Wizard
Editing Component Properties
Modifying GUI Source Code

Legal Notices