Java Bean Events

Responding to Events

Public methods

A bean responds to events using public methods.
However, these do not include the setters and getters (e.g. in the BeanBox builder tool. )

The SimpleBean2 has no public methods other than its setters and getters. Therfore it has nothing with which to respond to an event generated by another bean.

You can show this.

Suppose now that you want to add a button that will generate events for SimpleBean2.
The simplest way to make a bean responsive to incoming events from a button is to create some no argument public methods for the bean.

For example, we add a simple method in SimpleBean2 to get SimpleBean3. The latter has a new public method, dumb() which sets its msg field to "dumb".

For the BeanBox such methods must not have arguments. If dumb() has an argument, such as dumb(String s), the BeanBox won't recognize it. See SimpleBean4.

Adapters

You will notice that after the receiving method has been chosen (e.g dumb() in SimpleBean3) the BeanBox generates an adapter class which makes the connection.

Here is such a generated adapter class.


// Automatically generated an adapter file that handles ActionEvent.

package tmp.sunw.beanbox;
import SimpleBean3;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ___Hookup_162ba10135 implements java.awt.event.ActionListener, java.io.Serializable {

    public void setTarget(SimpleBean3 t) {
        target = t;
    }

    public void actionPerformed(java.awt.event.ActionEvent arg0) {
        target.dumb();
    }

    private SimpleBean3 target;
}


This adapter class implements the ActionListener interface by implementing the one method belonging to that interface, actionPerformed(ActionEvent).

The BeanBox calls setTarget(targetBean) to get a referece to the target bean (in this case SimpleBean3), and uses this reference to call the no argument public method dumb().

You can also write your own adapters (see the discussion in Chapter 3 of Java Beans by Robert Englander, O'Reilly, for additional details).

Creating an Event for a Bean

Most useful beans generate their own events.
To create such events you need to add methods to your bean to register and unregister clients for the events it generates.

Your bean needs to be able to fire its events (notify listeners) under the right circumstances.

You must also create corresponding event objects and corresponding listener interfaces that clients can implement.

You are really implementing a client server mechanism within one JVM. Your bean is a server, other beans are clients.

Example: a counter bean

As an example, here is a bean which counts up from 0 until some predefined number is reached.
At this point it either restarts the count, or fires an event.

It can receive counts from another bean, and the event it fires can be received by other beans.

The counter class has maxValue field, and there are methods that set and get this value: consequently, maxValue is a property that is displayed and can be changed in the "Properties" window (when counter bean is loaded into BeanBox).

Using the counter bean in the beanbox

To see the counter bean in action, do the following sequence:

You might want to change the interval property of the TickTock bean to 1 from 5 seconds (click on the bean and change the interval in the "Properties" window).

You can watch now the counter bean in action. However, with these settings nothing happens because the counter rollover property is set to true. So when the counter reaches 20 it just rolls over to 1 and starts again.

To get the counter to fire its event, change its boolean rollover property to false. The juggler stops when the count reaches 20.

This counter bean comes in three parts: