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:
- Change to connection mode and define sender and receiver
of the message:
- Click on the connection mode icon ()
to the right of the selection mode icon.
- In the form or the inspector window click first on the
sender bean jButton1, then on the receiver bean
jTextField3.
Now the connection wizard window pops up and asks for all the information
that will be needed to define the message.
- Select the source event that is the reason for the message
(here: the +-button has been pressed):
- From the list of event types open the action
folder and select actionPerformed.
- Confirm by clicking on the Next >
button.
- Define the kind of operation that will be done by the receiver
(namely its text property will be set):
- Make sure that the Set Property
radio button is active.
- Scroll down the list of possible properties of the text
field and select text.
- Confirm again with Next >.
- Determine the origin of the text parameter that will be
used to set the receiver's text property:
- From the radio button list of possibilities choose Property.
- Click on the ellipsis icon to the right. This opens a
property chooser window.
- Select jTextField1 from the
upper combo box. Now all properties of this component are displayed that
can be used to set the text property (i.e. that
have the correct type).
- Choose text from the properties
list and confirm with OK.
- Finally close the wizard with Finish.
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
- jTextField3.setText(jTextField1.getText());
This is easy to understand even with very little Java experience:
- Get the text from the text field jTextField1
and use it to set the text of jTextField3.
- To see whether this works just run the applet:
- Choose Build/Execute from the
main menu or simply press the execute button ().
A pop up window appears asking for access to the internal server. Answer
with Yes.
- The applet viewer starts and displays the running applet.
Unfortunately it doesn't use the size of the form, so it looks ugly. Simply
resize the window so that it looks similar to the design view.
- Enter any text into the first text field and press the +-button.
The text appears in the result field as requested. Try other text fields
or buttons to see that they have no effect.
- Finish the applet simply by closing the window. Afterwards
close the output window of NetBeans.
- 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:
- Open the filesystems window with Window/Filesystems.
- If necessary open the simpleDemo
directory.
- Double-click the simpleDemoApplet
html file ().
Its source code is now displayed in the source editor.
- Locate the APPLET tag and change
the width to 400 and the height
to 150.
- Save the changes with File/Save
or the save icon ().
- Close the filesystems window and switch to the form window
tab.
- Run the applet again to see that it now starts with the
correct size. Close it afterwards.
- Now define the correct message by manually editing the
message code:
- Switch to the Java source code tab and find the previously
defined message line.
- Replace it with the following four lines
- double val1 = Double.parseDouble(jTextField1.getText());
double val2 = Double.parseDouble(jTextField2.getText());
double result = val1 + val2;
jTextField3.setText(result + "");
- Save the changes and start the applet again. Enter numbers
into the input fields and press the +-button to see that it works. Close
the applet window afterwards.
- Finally create the messages for the three remaining buttons:
- Repeat the steps 1 - 4 exactly, only changing the receiving
button.
- Replace the generated message code with the four lines
from above. Change the operator + in the third
line to -, *, /
respectively.
- Run the applet and test it.
That we could not create the complete message without inserting manual code
was due to two difficulties:
- We had to combine input from two sources to compute the result.
- The text strings used for input and output had to be converted
to/from numerical values (doubles) to make arithmetic computations.
These problems have been addressed by the PhysBeans library:
- It contains function beans that have no visual counterpart,
but can be used to compute new results from several inputs.
- Many bean properties are defined as double values and the
conversion is done in the internal bean code.