Binding Data to a Swing Component

See Also

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 and JList components.

Before binding a component to a database, you need to have done the following things ready:

To create entity classes to represent the database that is to be bound to the JTable:

  1. In the Projects window, right-click your project and choose New > Other, select the Persistence category, and select the Entity Classes from Database template.
  2. In the Database Tables page of the wizard, select the database connection.
  3. Once the Available Tables column is populated, select the tables that you want to use in your application and click Add to move them to the Selected Tables column. Click Next.
  4. In the Entity Classes page of the wizard, make sure the Generate Named Query Annotations for Persistent Fields dialog box is selected.
  5. Make any customizations that you want to make to the names of the generated classes and their location.
  6. Click Create Persistence Unit.
  7. In the Create Persistence Unit dialog box, make sure of the following things:
  8. Click Finish.

    You should see nodes for the entity classes in the Projects window.

To bind the data to a JTable component:

  1. Right-click the component in the GUI Builder and choose Bind > elements.
  2. Click Import Data to Form. From the Import Data to Form dialog box, select the database table to which you want to bind your components. Click OK.
  3. From the Binding Source combo box, select the item that represents the result list of the entity class. For example, if the entity class is called, Customer.java, the list object would be generated as customerList.
  4. Leave the Binding Expression value as null.
  5. If there are any database columns that you want to appear in the JTable, select those columns in the Selected list and move them to the Available list.
  6. Select the Advanced tab to further configure the binding. For example, you can specify a validator or converter, or you can specify behavior if the binding source is null or unreadable.
  7. Click OK.

To bind the data to a JList component:

  1. Right-click the component in the GUI Builder and choose Bind > elements.
  2. Click Import Data to Form. From the Import Data to Form dialog box, select the database table to which you want to bind your components. Click OK.
  3. From the Binding Source combo box, select an item that represents the result list of the entity class. For example, if the entity class is called, Customer.java, the list object would be generated as customerList.
  4. Leave the Binding Expression value as null.
  5. In the Display Expression drop-down list, select the property that represents the database column that contains the values that you want to display in the list.
  6. Select the Advanced tab to further configure the binding.
  7. Click OK.
See Also
Binding Two Bean Properties
Special Binding Properties
Converting Values Between Source and Target Properties
Validating Target Value Changes in Bindings (Java Desktop Applications)
Adding a Component to a Form
Editing Component Properties
Designing Java GUIs with the GUI Builder
Adding a Bean to the Palette Window

Legal Notices