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:
- Connected to a database in the IDE.
- Added the component to a form in the GUI Builder.
- Created entity classes that represent the database tables to which you want to
bind. Entity classes are special classes that use the the
Java Persistence API (JPA).
Steps on creating the entity classes for binding data to a component
are given below.
To create entity classes to represent the database that is to be bound to the JTable:
- In the Projects window, right-click your project and choose
New > Other, select the Persistence category, and select the
Entity Classes from Database template.
- In the Database Tables page of the wizard, select the
database connection.
- 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.
- In the Entity Classes page of the wizard, make sure the
Generate Named Query Annotations for Persistent Fields dialog
box is selected.
- Make any customizations that you want to make to the
names of the generated classes and their location.
- Click Create Persistence Unit.
- In the Create Persistence Unit dialog box, make sure of the following things:
- That the selected Persistence Library is TopLink.
- That the selected Table Generation Strategy is "None".
- Click Finish.
You should see nodes for the entity classes in the Projects window.
To bind the data to a JTable component:
- Right-click the component in the GUI Builder and choose Bind > elements.
- 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.
- 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.
- Leave the Binding Expression value as null.
- 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.
- 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.
- Click OK.
To bind the data to a JList component:
- Right-click the component in the GUI Builder and choose Bind > elements.
- 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.
- 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.
- Leave the Binding Expression value as null.
- 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.
- Select the Advanced tab
to further configure the binding.
- Click OK.
- You can use the New Java Desktop Application wizard to
quickly create a whole working application that has CRUD
(create, read, update, and delete) features. You can also
drag a database table from the Services window on to a JTable to automatically
bind data to the JTable. 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.
- 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