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 Writing Event Listeners 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.
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 ); } }
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)
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); }
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:
jButton1
).
Notice that the button is highlighted in red when it is selected.
myBean1
component.
jButton1ActionPerformed1
] event by expanding the event type directory nodes,
as represented in the following figure.
yourName
property in the MyBean
component,
and click the Next button.
jTextField
component
from the Component combobox and choose the text property from the list
that is presented, as shown on the following figure.
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()); }