Trail: JavaBeans(TM)
Lesson: Manipulating Events

Event passing is the means by which components communicate with each other. Components broadcast events, and the underlying framework delivers the events to the components that are to be notified. The notified components usually perform some action based on the event that took place.

The event model was designed to accommodate the JavaBeans™ architecture. To understand how events and event handling work in the JavaBeans component model, you must understand the concepts of events, listeners, and sources. To refresh your knowledge in these areas, read the Event Handling lesson of the Swing tutorial.

The event model that is used by the JavaBeans architecture is a delegation model. This model is composed of three main parts: sources, events, and listeners.

The source of an event is the object that originates or fires the event. The source must define the events it will fire, as well as the methods for registering listeners of those events. A listener is an object that indicates that it is to be notified of events of a particular type. Listeners register for events using the methods defined by the sources of those events.

From the Properties lesson you discovered two event listeners. The PropertyChangeListener(in the API reference documentation) interface provides a notification whenever a bound property value is changed and the VetoableChangeListener(in the API reference documentation) creates a notification whenever a bean changes a constrained property value.

Simple Event Example

This example represents an application that performs an action when a button is clicked. Button components are defined as sources of an event type called ActionEvent(in the API reference documentation). Listeners of events of this type must register for these events using the addActionListener method.

Therefore, the addActionListener method is used to register the ButtonHandler object as a listener of the ActionEvent event that is fired by the button.

In addition, according to the requirements of the ActionListener class, you must define an actionPerformed method, which is the method that is called when the button is clicked.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextArea;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class ButtonHandler implements ActionListener {
   /**
   * Component that will contain messages about
   * events generated.
   */
   private JTextArea output;
   /**
   * Creates an ActionListener that will put messages in
   * JTextArea everytime event received.
   */
    public ButtonHandler( JTextArea output )
    {
        this.output = output;
    }

   /**
   * When receives action event notification, appends
   * message to the JTextArea passed into the constructor.
   */
    public void actionPerformed( ActionEvent event )
    {
        this.output.append( "Action occurred: " + event + '\n' );
    }
}

class ActionTester {
    public static void main(String args[]) {
        JFrame frame = new JFrame( "Button Handler" );
        JTextArea area = new JTextArea( 6, 80 );
        JButton button = new JButton( "Fire Event" );
        button.addActionListener( new ButtonHandler( area ) );
        frame.add( button, BorderLayout.NORTH );
        frame.add( area, BorderLayout.CENTER );
        frame.pack();
        frame.setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE );
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
    }
}

Using Introspection to Discover the Events A Bean Fires

The JavaBeans API provides event-oriented design patterns to give introspecting tools the ability to discover what events a bean can fire. For a bean to be the source of an event, it must implement methods that add and remove listener objects for that type of event. The design patterns for these methods are the following:

public void add<EventListenerType>(<EventListenerType> a)
public void remove<EventListenerType>(<EventListenerType> a)
These methods let a source bean know where to fire events. The source bean then fires events at those listener beans using the methods for those particular interfaces. For example, if a source bean registers ActionListener objects, it will fire events at those objects by calling the actionPerformed method on those listeners.
package java.awt.event;
import java.util.EventListener;

  public interface ActionListener extends EventListener {

  public void actionPerformed(ActionEvent e);
}

Using the NetBeans GUI Builder to Set Events

In the lesson "Using the NetBeans GUI Builder," you learned how to create a MyBean component, add the yourName property, and design a simple form. Now you will set an event by which a value entered in the JTextField component is stored in the yourName property. Use the GUI Builder as follows to set such an event:

  1. Left click the MyForm node.
  2. Switch to the Connection Mode by clicking the appropriate button on the GUI Builder toolbar.
  3. In the Design Area or Inspector window select the OK button (jButton1). Notice that the button is highlighted in red when it is selected.
  4. In the Inspector window select the myBean1 component.
  5. In the Connection wizard's Select Source Event page, select the action|actionPerformed[jButton1ActionPerformed1] event by expanding the event type directory nodes, as represented in the following figure.

    Selecting the action|actionPerformed[jButton1ActionPerformed1] event.

  6. Click the Next button.
  7. In the Specify Target Operation page, specify the yourName property in the MyBean component, and click the Next button.
  8. In the Enter Parameters page, specify the target property by selecting the Property radio button.
  9. Press the ellipsis (...) button to display the Select Property dialog box.
  10. In the Select Property dialog box select the jTextField component from the Component combobox and choose the text property from the list that is presented, as shown on the following figure.

    Selecting the action|actionPerformed[jButton1ActionPerformed1] event.

  11. Click the Finish button.

The Source Editor window is now displayed. Since the GUI Builder automatically generates the code to connect the form's components, the following code will be added to the MyForm class:

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        myBean1.setYourName(jTextField1.getText());
    }
Previous page: Previous Lesson
Next page: Bean Persistence