Tutorial: Building the Customer and Orders Master-Detail Form
Project: Cliffhanger Adventure Gear
Author:
Application Methods, Inc.
Description:
This document is a tutorial on how to recreate the Customer form used
in the Cliffhanger application. The Customer form displays a
master-detail view of a customer and orders placed by the customer.
Since master-detail views are common constructs in database application
design, this tutorial will illustrate by example how to construct master-
detail views with JBuilder.
This tutorial will describe how to use the JBuilder wizards to create a
new project and the framework for the various parts of the application.
The Application Wizard automatically generates a Frame class
for the application, which was used to create the Customer form. The
JBuilder DataModule Wizard was used to create a DataModule for the application,
and you'll learn how to use a DataModule in conjuction with other Frames in an application.
Customer UI
Step 1: Start your project with wizards
- Choose File|New Project to create a new Java project.
- Edit the File field to read:
[d:]\JBuilder\myprojects\CustomerBrowser\CustomerBrowser.jpr
where [d:] is the drive in which JBuilder is installed.
- Choose Finish.
- Choose File|New and double-click the Application icon to open the
Application Wizard.
Note: If you have no projects open when you start the
Application Wizard, JBuilder automatically runs the Project Wizard first to
create the project, then opens the Application Wizard.
- Change the class field on Step 1 of the Application Wizard:
Class: CustomerBrowser
- Click the Next button and change the following fields on Step 2:
Class: CustomerFrame
Title: Customers
- For this example you do not need a menu bar, a tool bar or an About Box. Check only the
options "Generate status bar" and "Center frame on screen".
- Click the Finish button.
- Choose File|Save All to save the project and its files. (It's a good idea
to save frequently during this tutorial, for example, at the end of each step.)
Before modifying the newly created CustomerFrame, now would be a good time
to create the DataModule since the CustomerFrame will need to access two
QueryDataSets from the DataModule.
Step 2: Create the Data Module
The following steps show how to create a new data module for the
application.
- Choose File|New and double-click the Data Module icon to create a new data
module.
- Use the default values for the fields.
- Click the OK button.
Next, the Database component must be added to the DataModule.
Step 3: Add the Database component
- Put the DataModule into Design mode: select DataModule1.java in
the Navigation pane (upper left) and click the Design tab at the bottom of
the AppBrowser window. This displays the visual design tools.
- Click the Data Access tab of the Component Palette.
- Select the Database component.
- Click on Data Access in the Component Tree to drop the Database
component into the DataModule.
- Select the Database component in the Component Tree, and click on
connection property in the Inspector. Click on the ellipses
button to bring up the connection property editor.
- Click the Choose URL button to select the Connection URL of the data
source for the application.
- From the list of available Connection URLs, select
jdbc:odbc:Cliffhanger.
If jdbc:odbc:Cliffhanger is not available in the list, click on
the Show data sources button to display a list of available ODBC data sources,
and select jdbc:odbc:Cliffhanger.
- Click on the OK button.
- Edit the following fields:
Username: SYSDBA
Password: masterkey
- Leave the "Prompt user password" option unchecked.
- Click on the Test connection button to make sure you have configured
the database connection correctly. If the test connection fails, see
Installing and setting up in the Database Application Developer's
Guide.
- Click on the OK button.
Next you will add the necessary QueryDataSet components to the DataModule.
Step 4: Add the QueryDataSets
You need to add one QueryDataSet component to access the Customer table in the
Cliffhanger database, and another QueryDataSet component to access the Orders
table. Then you'll have to link the two datasets in a master-detail relationship.
- Click the Data Access tab of the Component Palette.
- Select the QueryDataSet component.
- Click on Data Access in the Component Tree to drop the
QueryDataSet component into the DataModule.
- Select the queryDataSet1 component in the Component Tree, and
change the name property to
customerDataSet
.
- Click on query property in the Inspector, and click on the
ellipses button to bring up the query property editor.
- Select "database1" from the database drop-down list.
- Enter the following SQL statement in the SQL Statement text area:
SELECT * FROM Customer
- Click on the Test query button to test that the query executes.
- Click on the OK button.
- Add one more QueryDataSet component for the Orders dataset following the
steps above.
- Change the name property to
orderDataSet
.
- Select "database1" and set the SQL statement to the following:
SELECT ID, CUSTOMERID, ORDERDATE, STATUS
FROM Orders
WHERE CUSTOMERID = :ID
Don't test the query until the master-detail link is defined in the
following steps.
- Click on masterLink property in the Inspector, and click on the
ellipses button to bring up the masterLink property editor.
- Select customerDataSet from the Master Dataset drop-down list,
which will populate the Available Master Columns list, and default the
ID column in the Master Link Column list.
- Remove all columns from the Detail Link Columns list, and add the
CUSTOMERID column from the Available Detail Columns list to the
Detail Link Columns list.
- Click on the Test link button to test the master-detail link.
- Click on the OK button.
At this point, you have completed the necessary steps to establish the database
connection, and to access the data from the database. Next you will setup the
CustomerFrame to access the datasets from the DataModule.
Step 5: Access the DataModule
This step is the key to using a DataModule in a JBuilder project.
- Select CustomerFrame.java in the Navigation pane of the
AppBrowser.
- At the top of the file, you will see this code:
public class CustomerFrame extends DecoratedFrame {
BorderLayout borderLayout1 = new BorderLayout();
XYLayout xYLayout2 = new XYLayout();
BevelPanel bevelPanel1 = new BevelPanel();
Add the following line of code to get a handle to the DataModule so that
the QueryDataSets can be retrieved:
DataModule1 dm = DataModule1.getDataModule();
- Click the Design tab of the Content pane, if it's not already selected.
Now you're ready to add data-aware JBCL controls to the CustomerFrame to
display Customer records and related Order records. Although the DataModule
isn't visible in the Designer, the QueryDataSets defined in it will be available
when you're ready to bind controls to datasets.
Step 6: Add data aware controls
The following will only outline the steps to create the UI for the
CustomerFrame, for more details in creating a user interface with JBuilder,
see Designing a user interface in the JBuilder User's Guide.
The navigator panel
- Rename the original BevelPanel bevelPanel1 to
pnlNavigator
.
- Set the constraints property of pnlNavigator to
NORTH
.
- Add a new BevelPanel component to the CustomerFrame, name this
BevelPanel
pnlMain
.
- Set the constraints property of pnlMain to
CENTER
,
if it isn't already.
- Add a NavigatorControl and a ButtonControl to pnlNavigator.
- Change the layout property of pnlNavigator to
BorderLayout
, and set the constraints property of the
NavigatorControl and ButtonControl to WEST
and EAST
respectively.
- Change the margins property of the pnlNavigator to
4, 4, 4, 4
.
- Change the label property of the ButtonContol to
Close
- Set the dataset property of the NavigatorControl to
customerDataSet
.
- Go to the Inspector, select the Events page, and double-click the value
field for the actionPerformed event. Add the following code in the event
handler:
void buttonControl1_actionPerformed(ActionEvent e) {
this.setVisible(false);
this.dispose();
}
The main panel
- Add two BevelPanels to pnlMain, one on top and the other on the bottom.
- Rename the top panel
pnlCustomer
, and the other
pnlOrders
.
- Change the layout property of pnlMain to
BorderLayout
, and set the constraints properties of
pnlCustomer and pnlOrders, if necessary, to NORTH
and
CENTER
respectively.
The customer panel
- Add LabelControls and FieldControls and arrange them
to look like the screen shot of the CustomerFrame at the beginning of this
tutorial.
- Give the controls meaningful names, like
lblFirstName
and
txtFirstName
for example.
- Set the dataset property of the FieldControls to
customerDataSet
.
- Set the columnName property of the FieldControls to
the column names in the Customer table that you want to display in the UI.
- You can leave the layout property of pnlCustomer to the default
layout, which is the XY Layout. Optionally you can change it to the
GridBag Layout, which requires a little more fine tuning of the
constraints for each control on the panel to make the controls line up properly.
The orders panel
- Add a LabelControl at the top of pnlOrders, and set the
text property to
Orders placed by this customer:
- Add a GridControl in the middle of pnlOrders, and set the
dataset property to
orderDataSet
.
- Change the layout property of pnlOrders to
BorderLayout
, and set the constraints property of
the LabelControl and the GridControl to NORTH
and
CENTER
respectively.
Finally, set the dataset property of the StatusBar component to
customerDataSet
, so that all status messages from the
customer dataset will be displayed in the status bar.
Now try it out and see if it works. Run your program and browse through the
customers in the Cliffhanger database and see the corresponding order records
for each customer.
You can enhance the application by adding a few more things. Refer to the
source code of the Cliffhanger application for more implementation details:
- Add Persistent Columns for each of the QueryDataSets
in the DataModule. Then add Display Masks and Edit Masks for some of the
columns in the QueryDataSets, such as Date fields, Phone number
fields etc.
- Add a new Frame for viewing and maintaining Orders, then add two buttons
on the CustomerFrame, one to view a selected order, and the other to add a
new order for the current customer.
- Add code in the WindowClosing event to check if there are
pending edits yet to be posted and resolved, and prompt the user if they
would like to save the changes made.
- Add an ImageControl with a graphic image to spice up the UI.