Connecting the beans

Now that the graphical user interface is ready, the applet should do something. This happens when one or more beans send messages to other beans as a consequence of certain events. Such events are triggered by the user (a click on a button, an <ENTER> in a text field), by the program (at the program start or timer related) or by other beans (as messages). If a bean receives a message, it can react by changing its internal state or maybe by sending own messages to other beans.

In a JavaBeans environment one implements such behaviour by connecting two beans and defining the corresponding message. Using the Connection Wizard in NetBeans this task is reduced mainly - sometimes completely - to graphical operations with the mouse. Sometimes it is still necessary - or just simpler - to define the message content explicitely by adding a few lines of Java code manually.

In the case of our example applet the message flow is very simple:

To create the connection between the NumericTextLine for x and the Cart2PolarFunction proceed with the following steps:

  1. Change into the Connection Mode and specify the sender and the receiver of the message:
    • Click onto the Connection Mode icon 7493 at the right hand side next to the Selection Mode icon.
    • Go to the inspector window and click first onto the sender bean (numericTextLine1), then onto the receiver bean (cart2PolarFunction1). This opens the window of the Connection Wizard, which collects all information necessary to create the message code.
  2. Select the event that produces the message:
    • From the list of event types choose propertyChange in the class propertyChangeListener. This is the default event a NumericTextLine sends, when the user enters a new value.
    • Confirm with Next >.
  3. Define the kind of operation that is done by the receiver of the message (i.e. the x property is set):
    • Make sure that the option Set Property is selected.
    • From the list of properties of cart2PolarFunction1 choose x. It contains the current real value.
    • Again confirm with Next >.
  4. Specify where the new value for x (of data type double) comes from (here: from the value property of numericTextLine1):
    • Select the radio button Property: .
    • Click onto the icon 7512 to the right. This opens a selection box.
    • From the combo box choose numericTextLine1 as Component. The field below displays all its properties that can be used (i.e. that have the correct data type double).
    • Select the (only) item value and confirm with Ok.
    • Close the Connection Wizard with Finish.

This creates all Java code that is necessary for the message passing. Most of it is guarded code, namely the addition of a listener that waits for events coming from numericTextLine1:

numericTextLine1.addPropertyChangeListener(
  new java.beans.PropertyChangeListener() {
    public void propertyChange(java.beans.PropertyChangeEvent evt) {
      numericTextLine1PropertyChange(evt);
    }
  }
);

and the function called by the listener

  private void numericTextLine1PropertyChange(
    java.beans.PropertyChangeEvent evt
  ) {
    ...
  }

that makes the actual work. Only the body of this function - the message proper - is not guarded and can be adjusted manually.

After the wizard is finished NetBeans switches to the source view and jumps directly to the generated message

  cart2PolarFunction1.setX(numericTextLine1.getValue());

Even with little knowledge of Java one can understand its meaning:

Take the value of numericTextLine1 and use it to define the property x of cart2PolarFunction1.

This is exactly what was intended, therefore no manually change of the generated code is necessary.

Now switch back to the design view and create the second message, from numericTextLine2 to cart2PolarFunction1, in exactly the same way using the following parameters:

Similarly enter the message from cart2PolarFunction1 to numericTextLine3 using

And finally the last one from cart2PolarFunction1 to numericTextLine4:

This finishes the construction of the applet. To see whether everything is ok, just run it (as usual with Run/Run File or Shift-F6). NetBeans is aware of the code change since the last compilation and first compiles automatically, before it starts the applet. If you enter the test values -3.0 and -4.0 (and confirm both with <ENTER> !), you should see the following:

7552

Remarks:

1 This happens because the autoTriggered property of the Cart2PolarFunction has the (default) value true.

ZurückWeiter