Java Reference
In-Depth Information
As of J2SE 1.4, the JavaBeans specification recommends that a bean class
should contain the get<event_name>Listeners method, which returns an
array containing all registered listeners for the event. Adding this method
is not a requirement, but I would recommend adding its implementation,
especially if you are developing new JavaBeans.
Let's take a look at event handling in action. We will hook together the
Movie bean with a couple of JButton components, which are the source of
ActionEvents. Open the Bean Builder and follow along.
Step 9: Hooking up Buttons to the Movie Bean
Start with a new design window in the Bean Builder by clicking File, New
from the main menu. Load the movie_bean.jar file, and add a Movie bean to
the design window. Then add two JButton objects, as shown in Figure 19.20.
Change the label on one button to say Rent and the other button to say Return.
Click an event hookup button on the Rent button and drag it over to an
event hookup button on the Movie bean. The Interaction Wizard appears, as
shown in Figure 19.21. Click on action for the event and actionPerformed() for
the method, and then click the Next button.
Figure 19.22 shows the next step of the wizard, which prompts for the target
method in the Movie bean. I want the Rent button to cause the movie to be
rented, so select the rentMovie() method, as shown in Figure 19.22, and click
the Next button.
The next step of the wizard is prompting for a get method in the JButton
bean, but this does not apply in our situation, so you can simply click the Fin-
ish button to finish the wizard.
Perform similar steps for the Return button, except choose the method
returnMovie() as the target method for the action event.
Figure 19.20
Add a Movie bean and two JButton objects, changing the labels on the two
buttons.
Search WWH ::




Custom Search