![]() |
![]() |
In Building your first applet, you learned about making connections. In this section, you explore the different types of connections and what you can do with them. It is best to follow along in the Visual Composition Editor as the different connection types are described and to try any examples discussed. Creating and experimenting with connections is an excellent way to learn how to use them.
Note:
In Property-to-property connections, you create a
new applet. You can reuse this applet to follow along with all of the examples in this
section.
There are six types of connections:
Event-to-code and parameter-from-code connections enable you to connect to methods of the composite bean.
A connection has a source and a target. The point at which you start the connection is called the source. The point at which you end the connection is called the target. For information on connection properties, see Changing the properties of connections.
Note:
If a particular bean method, property, or event does not appear in the bean's preferred
feature list in its bean or connection pop-up menu, you can select Connect and then
Connectable Features from the bean pop-up or connection pop-up menu to display a
complete list. The list of methods, properties, and events displayed in a window opened by
selecting Connectable Features represents a bean's complete public interface.
Property-to-property connections tie two data values together. The color of this connection type is blue. A simple example of a property-to-property connection follows:
The free-form surface should look like this:
When you run the applet that contains these beans, the JLabel text becomes JTextField1.
For property-to-property connections, either endpoint can serve as the source or target. The only time it matters which property is the source and which is the target for a connection is at initialization. During initialization, the value of the target is updated to match the value of the source.
A property-to-property connection is initiated from the source bean's Connect choice in the pop-up menu and is terminated by selecting the appropriate target bean's feature.
Event-to-method connections cause a method to be called when a certain event takes place. The color of this connection type is green.
For event-to-method connections, the event is always the source and the method is always the target. A simple example of an event-to-method connection follows:
The free-form surface should look like this:
Properties can also be used to call a method, and events can be used to change the value of a property. This behavior is possible because VisualAge for Java can associate an event with a change in property value. As a result, you can make the following connections with properties:
In addition to calling a method, an event can also be used to set a property value. In this case, a parameter must be used with the connection to supply the property value. A simple example of an event-to-property connection follows:
The free-form surface should look like this:
When you run the applet and select the button, the title text of the frame is set to This is a JFrame title. This example is rather contrived, but it conveys the idea. For more information, see Connection parameters.
Event-to-code connections run a given method when a certain event takes place. This provides a way to implement or alter applet behavior directly through the use of the Java language. The target of an event-to-code method can be any method in the class that you are manipulating in the Visual Composition Editor.
Note:
An event-to-method connection is made between two beans. An event-to-code connection is
made between a bean and a method in the composite bean. The method in the composite been
does not have to be public.
The color of an event-to-code connection is green. To create an event-to-code connection:
The connection is drawn between the source bean and a movable text box that contains the name of the method.
The last three types of connections supply a parameter to a connection from various sources:
The color for a parameter connection line is purple.
Parameter-from-property connections use the value of a property as the parameter to a connection. As with other connection types, a parameter-from-property connection is initiated from the source bean's Connect choice in the pop-up menu and is terminated by clicking mouse button 1 over the target connection line requiring the parameter. You then select the appropriate property from the pop-up menu for the connection.
A parameter-from-property connection was used in building the To-Do List sample. When we connected the JTextField bean's text property to the connection between the JButton bean and the DefaultListModel bean, we were making a parameter-from-property connection. Refer to Connecting beans for the specific connection details for the To-Do List sample. The following example also illustrates the use of the parameter-from-property connection:
The free-form surface should look like this:
When you run the applet and type text into the text field and select the button, the label text is set to match the text in the text field.
Parameter-from-code connections run a method whenever a parameter to a connection is required. This connection is much the same as a parameter-from-property connection, except that the value supplied to the connection is returned from a Java method instead of a bean property value.
For the sake of illustration, assume that we have created a simple method called stringFromCode in the applet class that returns the text this is a string. An example of a parameter-from-code connection follows. Unlike other connection types, a parameter-from-code connection is initiated from the parameter name in the connection's pop-up menu and is terminated as follows:
Parameter-from-method connections use the result of a method as a parameter to a connection. An example of how to use a parameter-from-method connection follows:
When you test the applet, type a string into the text field and then select the Button. The string becomes the label for the Button.
Connections, like beans, have properties. To open the properties for a connection, select Properties from the connection's pop-up menu. Or, just double-click on the connection.
The following figure shows the Properties window for a property-to-property connection:
You can use a connection's Properties window to change the source or target property of the connection. To do this, select a different source or target property from the appropriate list. To display the current source and target properties of the connection, select Reset.
If you want the source property of a property-to-property connection to be the target property, and vice versa, you can change the source and target properties by selecting Reverse in the connection's Properties window.
When you have finished changing the connection's properties, select OK.
Event-to-method and event-to-code connections sometimes require parameters (or arguments). The method's parameters are available as properties of the connection. Therefore, to specify a parameter, you simply make a connection to the parameter property of the event-to-method connection itself.
When a connection requires parameters that have not been specified, it appears as a dashed line, indicating that the connection is not complete.
In the To-Do List applet, you connected the JButton bean's actionPerformed event to the DefaultListModel bean's addElement(java.langObject) method, and a dashed line resulted:
The parameters that methods require are indicated by the items inside parentheses () in the method name. For example, the addElement(java.lang.Object) method takes one parameter, an Object. A method named insert ElementAt(java.lang.Object, int) takes two parameters, an Object and an int.
When you have specified all of the necessary parameters, the connection line becomes solid, indicating the connection is complete. If you do not supply enough parameters for a connection, the connection continues to appear as a dashed line.
To specify parameters you can use properties or constants.
Most of the time, the parameters you need are properties of other beans you are working with in the Visual Composition Editor. To use a bean's property as a parameter:
While making a connection to a connection line, you will see a small visual cue in the middle of the connection line when the mouse pointer is directly over the connection line, indicating the pointer is positioned correctly.
In the To-Do List applet, the text entered in the JTextField bean is used as the parameter of the event-to-method connection between the Add Button and the DefaultListModel bean.
In this example, you provided the parameter of the event-to-method connection by making a property-to-property connection between the JTextField bean's text property and the event-to-method connection's obj property. The connection's obj property is the name of the first and only parameter of the addElement(java.lang.Object) method.
Parameter values can also be constants. You specify a constant value for a parameter in the Properties window for the connection.
For example, to specify a constant value for the parameter for an event-to-method connection:
The Properties window for an event-to-method connection opens:
Like beans, once connections are made, you can manipulate them in many different ways.
You select and deselect connections the same way you select and deselect beans. You can select multiple connections. The information about the currently selected connection is displayed in the information area at the bottom of the Visual Composition Editor.
Note:
You cannot select beans and connections at the same time.
To delete a connection, select Delete from its pop-up menu. You can also delete a connection by selecting the connection and pressing the Delete key.
To delete several connections, select the connections you want to delete, and then select Delete from the pop-up menu of one of the selected connections.
When you make several connections from the same event or property of a bean, the connections run in the order in which they were made. However, if you create the connections in a different order than the order in which you want to run them, you can reorder them. Add a JButton and two JFrame containers. Connect an actionPerformed event from the button to a show method at one frame. Make the same connection to the second frame. You now have two connections to open frames. When you run the applet, one frame will appear followed by the second after you click the button.
If you need to change the order of connections, simply reorder the connections from the bean by doing the following:
The Reorder connections window appears:
The Reorder connections window contains all of the connections for the bean you selected. In this example, the (JButton1, actionPerformed -> JFrame2, show() connection is the first one that runs.
As you drag a connection through the list, a dark line appears to indicate where the connection will be inserted when you release the mouse button. Rerun your applet to see the effect.
You can show and hide connections by using the Hide Connections tool and the Show Connections tool from the tool bar.
These tools show and hide all connections to and from the selected bean or beans. If no beans are selected, these tools will show and hide all the connections in the Visual Composition Editor.
You can selectively show and hide a bean's connections by selecting Browse Connections from the bean's pop-up menu and then selecting one of the following:
When you select a connection, selection handles are displayed at both ends and along the connection line. You can then drag the mid-point selection handle to a new position. This makes the connection line draw in a different area of the free-form surface, which can help you distinguish among several connection lines that are close together. When additional selection handles appear, you can then drag the middle selection handle to a new position to bend the connection line even further.
You can restore a connection line to its original shape. From the pop-up menu for the connection line, select Restore Shape.
VisualAge for Java gives you the ability to change the endpoint bean of a connection, meaning that you can change the source or target bean of the connection. It is quicker than deleting the connection and creating a new one.
For event-to-method and property-to-property connections, you can move either end of the connection. For event-to-code connections, you can only move the event end of the connection.
To change either end of a connection:
If you move the endpoint of a connection to a bean that does not have the same property available as in the original connection, the bean's connection pop-up menu appears so you can specify a new property to connect to.
![]() |
![]() |