Connecting Beans

Now that the interface is ready, we have to add behaviour. This is done via message passing between beans. For that purpose each bean has a list of events to which it can react. Events can be initiated by the user (a click on a button or an <ENTER> in a text field) or they can be system generated (timer events or events generated by other beans). When an event occurs, a bean can react directly and/or send an appropriate message to another bean, which in turn reacts to the incoming message.

In a Java Beans environment one implements this by drawing connections between beans and defining the proper messages. In many cases this can be done completely in a graphical way, but sometimes it is necessary - or even simpler - to put in a few lines of Java code that define the concrete message.

In our example applet we will need four almost identical messages. One of them can be defined as a connection between the plus button and the result text field and can be formulated as:

"When I (the button) am pressed, you (the output field) have to get the numbers from the two input fields, add them and display the result."

This message is a bit too complicated to be created graphically, though this would be possible by inserting an intermediate bean for addition. To see what is possible without any hand code, we first implement the following simpler message:

"When I (the button) am pressed, you (the output field) have to get the number from the first input field and display it."

and will then change it to work as requested.

To implement all this with NetBeans, you need the following steps:

  1. Change to connection mode and define sender and receiver of the message: Now the connection wizard window pops up and asks for all the information that will be needed to define the message.
  2. Select the source event that is the reason for the message (here: the +-button has been pressed):
  3. Define the kind of operation that will be done by the receiver (namely its text property will be set):
  4. Determine the origin of the text parameter that will be used to set the receiver's text property: This creates all code that is needed for the message transfer and switches to the message code in the source editor. Most of the code is "guarded", which means, it is completely under the control of NetBeans and can not be changed manually in the source editor. It is marked by a light blue background. The message itself is not guarded, it is the simple line This is easy to understand even with very little Java experience:
  5. To see whether this works just run the applet:
  6. To start the applet NetBeans has automatically created a simple html file for it. You can edit it to set the requested applet size for future runs:
  7. Now define the correct message by manually editing the message code:
  8. Finally create the messages for the three remaining buttons:

That we could not create the complete message without inserting manual code was due to two difficulties:

These problems have been addressed by the PhysBeans library: