This guide is an introduction to the support in NetBeans IDE 6.0 for beans binding and data binding in Java desktop applications.
Contents
|
For this tutorial you need to have the following software installed on your computer:
Until the release of the beans binding library, it was somewhat cumbersome to connect UI components to databases or to keep values of component properties in sync. For example, displaying data from a standard database in a JTable required the manual creation of utility classes to handle the connection between the database and the JTable. And keeping values of different bean properties in sync (such as the value of a JTextField with the rendering of a visual bean) required hand-coding of listeners and event handlers.
The beans binding library simplifies and standardizes all of this. You can merely write a few lines of code to establish which properties of which components need to be kept in sync, and the beans binding library handles the rest. In the NetBeans IDE, beans binding features are integrated in the GUI Builder, so you can quickly get the behavior of your application coded soon after you have established the visual design.
This guide is an overview of the main aspects of beans binding in the IDE. For a concrete example of many of these features, you can try the Building a Java Desktop Database Application tutorial.
At its most basic, beans binding is a way to connect bean properties without using event listening and handling code.
To illustrate the concept of beans binding and how the IDE supports it, we will do a simple example where a user can adjust a slider to change a numerical value in a text field.
To set up the example:
(If JFrame Form is not available in the New menu, choose Other. Then in the New File wizard, select the Swing GUI Forms category and select the JFrame Form template.)
NumberSliderForm.java should open in design mode in the editing area.
The resulting form might look something like the screenshot below. However, positioning is not important for purposes of this example.
Now that we have set up the example, we are ready to create the binding. However, first we need to determine which component will be the source of the binding and which will be the target. The binding source component is where a value for the property first originates.
When binding in the GUI Editor, you initiate a binding on the target and then you declare the source in the Bind dialog box.
In this case, since the JSlider comes with a default range of values, we will use it as the source.
Note: Bindings can be two-way (read/write), so that changes in the target are automatically reflected in the source. However, the direction of the initial binding is always from the source to the target. See the information on Update Mode in the Advanced Binding Configuration section.
To bind the slider to the text field:
You have just bound the value bean property of the slider to the text value of the text field.
In the design area, the text field should show the value 50. This value reflects the fact that the slider is in the middle position and the default range of values for the slider is from 0 to 100.
You can now run the application and see the binding in action.
To run the project:
The applications should start in a separate window. Adjust the slider in the running application and watch the value change in the text field.
The example above shows a straightforward binding with some default behaviors. But sometimes you might want or need to configure your binding differently. If that is the case, you can use the Advanced tab of the Binding dialog box.
The Advanced tab of the dialog box contains the following fields:
The Converter drop-down list is populated with any converters that have been added as beans to your form. You can also add the conversion code directly by clicking the ellipsis (...) button, and selecting Custom Code from the Select Converter Property Using drop-down list.
Validators need to extend the org.jdesktop.beansbinding.Validator class.
The Validator drop-down list is populated with any validators that have been added as beans to your form. You can also add the validation code directly by clicking the ellipsis (...) button, and selecting Custom Code from the Select Validator Property Using drop-down list.
Note: To better understand the classes and methods mentioned above, you can access the beans binding Javadoc documentation directly from the IDE. Choose Help > Javadoc References > Beans Binding. In the browser window that opens, click the org.jdesktop.beansbinding link to access documentation for those classes.
Once you have created a new Java form and added components to the form, you can generate code to bind those components to data. The IDE makes it easy to bind data to Swing JTable, JList, and JComboBox components.
Before binding a component to data from a database, you need to have done the following things:
To create entity classes to represent the database that is to be bound to the JTable:
You should see nodes for the entity classes in the Projects window.
To bind the data to a JTable component:
To bind the data to a JList component:
Note: You can also use the New Java Desktop Application wizard to quickly create a whole working application that has CRUD (create, read, update, and delete) features. However, it is better to generate all the entity classes in advance to ensure that all relations among the entities are correctly covered by the generated classes.
Where necessary, the beans binding library provides special synthetic properties for some Swing components that are missing from the components themselves. These properties represent things, such as a table's selected row, that are useful to bind to other properties.
Below is a list of the synthetic properties added by the beans binding libraries:
Component | Property | Description |
---|---|---|
AbstractButton | selected | The selected state of a button. |
JComboBox | selectedItem | The selected item of a JComboBox. |
JSlider | value | The value of a JSlider; notifies of all changes. |
value_IGNORE_ADJUSTING | Same as "value" but does not notify of change while the slider is adjusting its value. | |
JList | selectedElement | The selected element of a JList; notifies of all changes. If there is a JListBinding with the JList as the target, the selected element is reported as an element from the binding's source list. Otherwise, the selected element is reported as an object from the list's model. If nothing is selected, the property evaluates to null. |
selectedElements | A list containing the selected elements of a JList; notifies of all changes. If there is a JListBinding with the JList as the target, the selected elements are reported as elements from the binding's source list. Otherwise, the selected elements are reported as objects from the list's model. If nothing is selected, the property evaluates to an empty list. | |
selectedElement_IGNORE_ADJUSTING | Same as "selectedElement" but does not notify of change while the list selection is being updated. | |
selectedElements_IGNORE_ADJUSTING | Same as "selectedElements" but does not notify of change while the list selection is being updated. | |
JTable | selectedElement | The selected element of a JTable; notifies of all changes. If there is a JTableBinding with the JTable as the target, the selected element is reported as an element from the binding's source list. Otherwise, the selected element is reported as a map where the keys are composed of the string "column" plus the column index and the values are the model values for that column. Example: {column0=column0value, column1=column1value, ...} If nothing is selected, the property evaluates to null. |
selectedElements | A list containing the selected elements of a JTable; notifies of all changes. If there is a JTableBinding with the JTable as the target, the selected elements are reported as elements from the binding's source list. Otherwise, each selected element is reported as a map where the keys are composed of the string "column" plus the column index and the values are the model values for that column. Example: {column0=column0value, column1=column1value, ...} If nothing is selected, the property evaluates to an empty list. | |
selectedElement_IGNORE_ADJUSTING | Same as "selectedElement" but does notify of change while the table selection is being updated. | |
selectedElements_IGNORE_ADJUSTING | Same as "selectedElements" but does not notify of change while the table selection is being updated. | |
JTextComponent (including its sub-classes JTextField, JTextArea, and JEditorPane) | text | The text property of a JTextComponent; notifies of all changes (including typing). |
text_ON_FOCUS_LOST | The text property of a JTextComponent; notifies of change only when focus is lost on the component. | |
text_ON_ACTION_OR_FOCUS_LOST | The text property of a JTextComponent; notifies of change only when the component notifies of actionPerformed or when focus is lost on the component. |
For a more general introduction to using the IDE's GUI Builder, see GUI Building in NetBeans IDE.
To see how you can use the Java Desktop Application project template to build a database application with a Master/Detail view, see Building a Java Desktop Database Application.
For more information on Beans Binding, see the Beans Binding project page on java.net.
For general information on JavaBeans components, see the Beans trail of the Java Tutorial.