Implement beans and Data Binding in desktop applications

Source: Internet
Author: User
Document directory
  • Source and target
  • Create object class
  • Bind the component to the bean that represents the data
Implement beans and Data Binding in desktop applications
By, 11/6/07    

This tutorial describes how to bind beans and data in a Java Desktop Application through netbeans ide 6.0.

Content

- Introduction: bean binding in netbeans ide 6.0
- Bind multiple attributes
- Advanced binding attributes
- Bind data to components
- Special binding property
 
Software and documentation requirements for this tutorial

To complete this tutorial, install the following software on your computer:

  • Netbeans ide 6.0 (download ).
  • Java standard development kit (JDK) version 5.0 or 6.0 (download)

Introduction: bean binding in netbeans ide 6.0

When bean binding libraries are not released, it is usually difficult to connect the UI component to the database or synchronize the component property values. For example, to display data from a standard database in jtable, You need to manually create a category to process the connection between the database and jtable. To keep the values of different bean attributes (for example, the jtextfield value of the visible bean) synchronized, you need to write the listener and event handler in person.

The bean binding library simplifies and standardizes all these operations. You only need to write several lines of code to determine which components need to be synchronized. The rest of the work is done by the bean binding library. In netbeans IDE, the bean binding feature is integrated into GUI builder. Therefore, after determining the visual design, you can write the behavior of the application immediately.

This tutorial provides an overview of the main aspects of IDE bean binding. To learn more about specific applications with many features, read the tutorial on building a Java Desktop database application.

Bind multiple attributes

Essentially, bean binding is a method that connects bean attributes without listening to events and processing code.

To explain the concept of bean binding and the support provided by IDE, a simple example is provided. You can adjust the slider to modify the numeric value in the text field.

The example settings are as follows:

  1. In IDE, select File> new project.
  2. Select the Java category and select the Java application template. Click Next.
  3. On the name and location pages of the wizard, perform the following operations:
    • InputNumbersliderAs the project name.
    • Select the set as main project check box.
    • Deselect the create main class check box.
  4. Click Finish to exit the wizard and set the project.
  5. In the projects window, right-click the numberslider project node and choose new> jframe form.

    (If the jframe form in the new menu is not available, select other. In the new file wizard, select the swing GUI forms category and select the jframe form template ). </P>

  6. On the name and location page of the wizard, perform the following operations:
    • InputNumbersliderframeAs the class name.
    • InputNumbersliderAs the package name.

  7. Click Finish to exit the wizard and create a form.

    Numbersliderform. JavaIt will be opened in design mode in the editing area.

  8. In the swing controls area of the palette just displayed, drag the slider control to the design area.
  9. Drag the text field component in the palette to the design area.

    The generated form may be similar to the screen shown below. However, the placement of the control does not affect the demonstration purpose.

Source and target

Now we have created an example to start creating a binding. However, we first need to determine which components are boundSourceAnd BindTarget. The attribute value in the binding source is used as the initial value of the binding.

When binding in the GUI editor, the target component is bound and the binding source is declared in the BIND dialog box.

In this example, because jslider provides the default value, we use it as the binding source.

Note:Binding can be bidirectional (read/write), so changes made to the target are automatically reflected in the source. However, the initial binding direction is always from the source to the target. For information about update mode, see the advanced binding configuration section.

To bind the slider to a text field:

  1. Right-click the text field component and choose bind> text to open the BIND dialog box.
  2. Select from the binding source combo boxJslider1.
  3. Select from the binding expression combo boxValue int, As shown in.

  4. Click OK.

You haveValueBean property bound to the text fieldTextValue.

In the design area, the text field should display the value50. This value indicates that the slider is in the center, and the default value range is 0 to 100.

Now you can run the application and view the binding operation.

To run a project:

  1. Select Run> run main project.
  2. In the run project dialog box, click OKNumberslider. numbersliderformSet as the primary class.

The application will be started from a separate window. Adjust the slider in the running application and check whether the value of the text field changes.

Advanced binding Configuration

The above example shows simple binding with default behavior. However, you may want to configure different bindings. If so, you can use the Advanced tab of the binding dialog box.

The Advanced tab of the dialog box contains the following fields:

  • Name.This allows you to create a name for the binding, allowing you to manage the binding more flexibly. The bound name is added to the bound constructor and can be boundGetname ()Method reference.
  • Update mode.Specifies the method for Attribute synchronization. Possible values include:
    • Always perform synchronization (read/write ).Whenever a change is made to the source or target, the other party will be updated accordingly.
    • Read only from the source (read only ).The target is updated only when the source value is set for the first time. When the source is modified, the target is updated. The changes made to the target are not updated to the source.
    • Read the source only once (read only once ).Update the target only when the target is bound to the source for the first time.
  • Update source when(Only for the jtextfield and jtextarea ComponentsTextAttribute ). This allows you to select the frequency of synchronization with attributes.
  • Ignore Adjusting(OnlyValueAttributes, jtable, and jlistSelectedelementAttributes, jtable, and jlistSelectedelementsAttribute ). If this check box is selected, all changes to an attribute will not be passed to the corresponding Attribute before the user completes the change. For exampleValueThe value of the property bound to the property is updated only after the user releases the mouse.
  • Converter.If the binding contains attributes of different data types, you can specify the code to convert values between types. The beans binding library can handle many common conversions. However, you need to provide your own conversion programs for combinations of other attribute types. This type of conversion program needs to be extendedOrg. jdesktop. beansbinding. ConverterClass.

    You can fill the converter drop-down list with any conversion program that is added to the form as bean. You can also directly add the conversion code by clicking the ellipsis (...) button, and then selecting custom code from the select converter property using drop-down list.

  • Validator.This allows you to instruct the code to verify the change to the target property value, and then pass the change back to the source property. This verification program needs to be extendedOrg. jdesktop. beansbinding. validatorClass.

    The validator drop-down list can be filled by any validators added to the form as beans. You can also directly add the verification code by clicking the ellipsis (...) button, and then selecting custom code from the select validator property using drop-down list.

  • NULL Source value.If the source property hasNullThis field allows you to specify a different value. This field correspondsOrg. jdesktop. beansbinding. BindingClassSetsourcenullvalue ()Method.
  • Unreadable source value.If the binding expression cannot be parsed during binding attempt, this field allows you to specify a different value for use. This field correspondsOrg. jdesktop. beansbinding. BindingClassSetsourceunreadablevalue ()Method.
  • To better understand the classes and methods mentioned above, you can bind the javadoc document directly using the IDE method beans. Select help> javadoc references> beans binding. In the open browser window, clickOrg. jdesktop. beansbindingLinks can access documents of these classes.

Bind data to components

After creating a new Java form and adding components to it, you can generate code to bind data to these components. With IDE, you can easily bind data to the swing jtable, jlist, and jcombobox components. </P>

Before Binding data in a database to a component, you must perform the following operations:

  • Connect the IDE to a database.
  • Add a component to a form in Gui builder.
  • Create a class that represents the database table to be bound. The following describes how to create an object class that binds data to a component.
Create object class

To create an object class and bind it to the jtable database, perform the following operations:

  1. In the projects window, right-click the project, select new> Other, select the persistence category, and select entity classes from the database template. </LI>
  2. On the database tables page of the wizard, select database connection.
  3. After filling in the available tables column, select the tables you want to use in the application, click Add to move them to the selected tables column, and click Next.

  4. On the entity classes page of the wizard, make sure that the generate named query annotations for persistent Fields dialog box is selected.
  5. Customize the name and position of the generated class as needed.

  6. Click Create persistence unit.
  7. In the create persistence unit dialog box, make sure the following options are selected:
    • The selected persistence library is toplink.
    • The selected table generation strategy is "NONE ".

  8. Click Finish.

    You should be able to see entity class nodes in the projects window.

Bind the component to the bean that represents the data

To bind data to the jtable component:

  1. Right-click the component in Gui builder and choose bind> element.
  2. Click Import data to form. Select the database table for which you want to bind components from the import data to form dialog box. Click OK.
  3. In the binding source check box, select an item that represents the object class result list. For example, if an object class is calledCustomer. Java, GenerateCustomerlistList object.
  4. The reserved binding expression value isNull.
  5. If you want to display some database columns in jtable, select these columns from the selected list and move them to the available list.
  6. Select the Advanced tab to further configure binding. For example, you can specify a validators or conversion programs, or specify behavior when the binding source is null or unreadable.
  7. Click OK.

To bind data to the jlist component:

  1. Right-click the component in Gui builder and choose bind> element.
  2. Click Import data to form. Select the database table for which you want to bind components from the import data to form dialog box. Click OK.
  3. Select an item that represents the object class result list from the binding source check box. For example, if an object class is calledCustomer. Java, GenerateCustomerlistCATEGORY object.
  4. The reserved binding expression value isNullUnchanged.
  5. In the display expression drop-down list, select the attribute indicating the database columns. These database columns contain the values you want to display in the list.
  6. Select the Advanced tab to further configure binding.
  7. Click OK.

Note:You can also use the new Java Desktop Application Wizard to quickly create an application with the complete functions of the crud feature (create, update, and delete. However, it is best to create all entity classes in advance to ensure that the generated classes cover all the relationships between entities.

Special binding property

If necessary, you can use the bean binding library to manually create special properties for some of the missing swing components in the component. The content represented by these attributes (such as the selected table row) can be used to bind attributes.

The following list shows some manually created attributes added to the bean binding Library:

Components Attribute Description
Abstractbutton Selected The status of the selected button.
Jcombobox Selecteditem The selected jcombobox item.
Jslider Value Value of jslider; notify all changes.
Value_ignore_adjusting It is the same as "value", but it is not notified when the slider adjusts its own value.
Jlist Selectedelement The selected jlist element notifies all changes. If jlistbinding uses jlist as the binding target, the selected element is reported as an element in the binding source list. Otherwise, the selected element is reported as an object in the list model. If no element is selected, the property value isNull.
Selectedelements Contains the list of the selected jlist elements. All changes are notified. If jlistbinding uses jlist as the binding target, the selected element is reported as an element in the binding source list. Otherwise, the selected element is reported as an object in the list model. If no element is selected, the attribute value is an empty list.
Selectedelement_ignore_adjusting It is the same as "selectedelement", but the changes are not notified when the list is updated.
Selectedelements_ignore_adjusting It is the same as "selectedelements", but it is not notified of changes when you select in the update list.
Jtable Selectedelement Specifies the jtable element selected. All changes are notified. If jtablebinding uses jtable as the binding target, the selected element is reported as an element in the binding source list. Otherwise, the selected element is reported as a ing. The key consists of the string "column" and column index, and the value is the model value of the column. For example: {column0 = column0value, column1 = column1value,...} if no element is selected, the attribute value isNull.
Selectedelements Contains a list of the selected jtable elements. All changes are notified. If jtablebinding uses jtable as the binding target, the selected element is reported as an element in the binding source list. Otherwise, the selected element is reported as a ing. The key consists of the string "column" and column index, and the value is the model value of the column. For example: {column0 = column0value, column1 = column1value,...} if no element is selected, the attribute value is an empty list.
Selectedelement_ignore_adjusting It is the same as "selectedelement", but the change is not notified during the table selection update.
Selectedelements_ignore_adjusting It is the same as "selectedelements", but the change is not notified during the table selection update.
Jtextcomponent (including its subclass jtextfield, jtextarea, and jeditorpane) Text The text attribute of jtextcomponent, which notifies all changes (including input ).
Text_on_focus_lost The text attribute of jtextcomponent. changes are notified only when the focus of the component is lost.
Text_on_action_or_focus_lost The text attribute of jtextcomponent. changes are notified only when the component notifies actionreceivmed or the component loses focus.

Feedback to us



Conclusion

For more comprehensive ide GUI builder usage information, see GUI building in netbeans ide 5.5. For more information about beans binding, see the beans binding project page on java.net.

 

 

Note: The bound path cannot contain Chinese characters.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.