Binding buttons
You can use the visual editor to bind the action of a Swing JButton to perform a service made available by a data source.
A Swing JButton is typically used to perform some action. The Java visual editor provides a binder class (SwingDataServiceAction) that you can use to program a button to perform a specific service on a data source. To configure the action binder for a button, you simply name the data source and the source service, as well as any parameter required by the service. In addition, the action binder can also set the component's initial state and change the state if the argument object for the action changes. For example, a button might be used to update an employee's name after it has been changed in a text field. The action binder for the button could enable the button when it detects that the last name is changed.
The following screen shot shows a button that is bound to a service provided by a Web service data source. The visual editor draws a dotted line from the action binder's argument object to the binder line. The icon on the line represents the SwingDataServiceAction binder for the button.
To configure an action binder for a button:
- In the design view, select a JButton and click the Bind tab that displays on the button. The Component Action Bindings dialog opens.
- In the Name field, type a name for the new action binding (the new instance of SwingDataServiceAction). You can skip this step and let the dialog suggest a name for the binding based on the data source service that you select.
- In the Source type drop-down list, select one of the following types, corresponding to the type of data source that is providing the service:
- EJB: Select this option if you are using (or are going to create) a data source based on an existing enterprise bean.
- Web Service: Select this option if you are using (or are going to create) a data source based on an existing Web service.
- Java Bean Factory: Select this option if you are using (or are going to create) a data source based on a basic Java bean factory.
- (Other types that you contribute to the Java visual editor that implement the IDataSource interface)
The Data source drop-down list includes all the data sources of the specified type that are defined in your application.
- In the Data source drop-down list, select the data source that you want to use to provide the new data object.
If no data sources exist, click the ... button next to the Data source field to define a new data source of the type specified:
- EJB data source: A wizard looks for existing EJB references in your project and helps you to define a new data source based on the EJB reference that you select.
- Web service data source: A dialog looks in your project for existing Web service clients, or proxies. If a proxy already exists, you can select the Web service and click Finish to add the data source. You can also click Add new Web service to start the Web Service Client wizard, which helps you define a new proxy for your project based on a WSDL document.
- Java Bean Factory data source: The Choose a Bean dialog box opens, where you can specify the Java bean factory class that you want to use as a data source.
- In the Source service list, select the method, or service, that you want the button to execute.
- If the service requires a parameter, select an object in the Argument drop-down list, and, if necessary, select the specific property to use for the parameter. For example, a service might require a java.lang.Integer as the parameter, so you might select another existing data object in your application that includes an ID property that is an integer.
Note: The OK button is only enabled if a compatible argument (or argument and property) is specified.
Restriction: Only services that take one or fewer parameters are supported by the provided binder classes. If you are working with a service that takes many parameters, you could create a wrapper service that takes an object containing all required arguments.- In the Initial state drop-down list, select one of the following options:
- Disabled: This choice sets the button component to be initially disabled, or greyed-out.
- Enabled: This choice sets the button component to be intially enabled, meaning it can be clicked.
- In the Behavior when the argument of the action object changes drop-down list, select one of the following options:
- ENABLE_ACTION: This choice indicates that the button will be set to enabled when the argument object changes.
- DISABLE_ACTION: This choice indicates that the button will be set to disabled when the argument object changes.
- NO_ACTION: This choice means that the state of the button will not change when the argument object changes.
- Click OK.
Parent topic
Binding Java visual components to data with the visual editor