Beans Binding Between Separate Forms
By dananourie on Jun 02, 2008
by Patrick Keegan
Continuing from my last post, I'll show the next steps in the creation of this simple (but not too simple) client purchase application. This time, our main focus is in creating a separate dialog which we will use for data entry. We'll need to do a few tricks so that input from the dialog is propagated to the main form and then the database.
But first we'll need to clear up a few loose ends. As I alluded to last time, I use AUTO_INCREMENT attributes for the ID columns of the CLIENTS, COUNTRIES, and ORDERS tables. This means that whenever a new row is added to those tables, that row's AUTO_INCREMENT field is given a unique value (the value of the last new record + 1). For me using AUTO_INCREMENT is a handy way to ensure having unique records.
When you generate the skeleton of the application in the New Java Desktop Application wizard, the IDE generates two entity classes (Clients.java and Orders.java) that represent database tables with the same names. However, these entity classes are missing code to deal with the AUTO_INCREMENT fields. Without that code, you will get errors when trying to enter new records. To fix that:
- Open the Clients.java class.
- Navigate to the line after the one containing
- Enter the line
- Press Ctrl-Shift-I to generate the necessary import statements for this annotation.
- Repeat the process for Orders.java.
Note: You can also use code completion here. It takes three selections to get the entire line, but the import statements are generated for free.
Once you have added these annotations, you can run the application and start adding data. Click the top New button to add a client. With a client selected, click the bottom New button to add an order for that client. Click Save to push your changes to the database. Click Refresh to back out any unsaved changes.
I like tables for browsing data, but I think they leave something to be desired for data entry. So for this application, we'll add dialogs for data entry.
To create and populate the JDialog, follow these steps:
- Right-click the package that contains your classes and choose New |
Other. Select Swing GUI Forms | JDialog Form template and name it
- From the Palette window drag, drop, and arrange components for the customer's personal details.
You should have JLabels for each of the following fields: first name, last name, address, city, state, zip code, country, and phone number. You should have JTextFields for each of those fields, except for country, for which we will use a JComboBox.
- Edit the display text for JLabels.
- Add two buttons and name them Save and Cancel.
- (Optional) Rename all of the components you have added to more memorable names, such as
firstNameLabel. You can do this inline in the Inspector window.
The resulting layout should look something like what you see below.
Read the rest of this tutorial.