This introductory tutorial steps you through creating a basic database application and user interface (UI) using the JBuilder design tools to read data from a text file. You can create a database application even if you are not connected to a SQL or desktop database. You can create the database application using data in a text file (in this case, a data file that ships with JBuilder) and the DataExpress TextDataFile and TableDataSet components. This tutorial then explores the JBuilder DataExpress architecture and applies database concepts to the tutorial even though this application does not connect to a SQL database.
The UI for this application consists of three components: a table component that displays the data from the text file, a toolbar to aid in browsing through the data, and a status area that displays messages.
The first step when creating an application in JBuilder is to set up the framework for the application. JBuilder's visual design tools can be used to quickly create the application structure and, in future steps of this tutorial, to read data from the text file and to develop the user interface.
To create the application structure,
Now that the basic project structure is created, let's start the Application wizard to create a new Java application shell which contains a Frame, a new UI container.
When importing data from a text file, as we are doing in this tutorial, two DataExpress components are needed:
To add the DataExpress components to your application,
To do this,
An entry for an instance of a TextDataFile object appears in the DataExpress folder of the structure pane as textDataFile1.
To do this,
An entry for an instance of a TableDataSet object appears in the DataExpress folder of the structure pane as tableDataSet1.
The next step is to connect the DataExpress components so that they can "talk" to each other. Setting the appropriate component properties connects the DataExpress components. Properties are set from the Properties tab of the Inspector. The Inspector displays the properties for the component selected in the UI designer (for UI components) or in the structure pane (for both UI and DataExpress components when the Design page is displayed). For more information on using the Inspector, see "Getting Started: Setting component properties in the Inspector" in the Programmer's Guide.
To get the two DataExpress components "talking" to each other, set the fileName property of the TextDataFile component. This tells JBuilder where to find the text file that contains the data to be read into textDataFile1. Since TextDataFile is a DataExpress component, select it from the structure pane rather than from the UI designer.
To set the fileName property of the TextDataFile component,
textDataFile1.setFileName("/usr/local/jbuilder/samples/com/borland/samples/DataExpress/TextDataFile/employee.txt");
The sample files are installed beneath the samples subdirectory of your JBuilder installation directory. For example, /usr/local/jbuilder/samples/DataExpress/. You may need to browse to find the appropriate samples directory on your computer.
The next step is to connect tableDataSet1 to the textDataFile1 component.
tableDataSet1.setDataFile(textDataFile1);
The employee.txt file is designed to work with the default settings stored in the TextDataFile component for properties such as delimiter, separator, and locale. If it was designed differently, the appropriate properties could be set at this point.
The JBuilder UI designer is used to build the user interface for this application. The following steps add UI elements to the application: a JdbTable (and its surrounding TableScrollPane), a JdbNavToolBar, and a JdbStatusLabel. The JdbTable is used to display two-dimensional data, in a format similar to a spreadsheet. The JdbNavToolBar is a set of buttons that help you navigate through the data displayed in a JdbTable. The JdbStatusLabel displays the current record and any error messages.
The first step in setting up a user interface is to determine the appropriate layout for your application. To learn more about layouts, see "Using layout managers" in the Programmer's Guide.
If you want to change the layout,
To add UI components to your application,
The JdbNavToolBar component enables you to move quickly through the data set when the application is running.
Among other information, the status bar displays information about the current record or current operation.
Scrolling behavior is not available by default in any Swing component or dbSwing extension, so, to get scrolling behavior, we add the scrollable Swing or dbSwing components to a JScrollPane or a TableScrollPane. TableScrollPane provides special capabilities to JdbTable over JScrollPane. See the dbSwing documentation for more information.
The Structure pane in the lower left pane of the AppBrowser now has an entry for the JdbTable, jdbTable1. JBuilder creates the corresponding source code immediately for the elements you've added or modified in your application. To see this code, click the Source tab.
The next step is to connect the DataExpress components to the UI components. For more information on the fine points of UI design, see "Designing a user interface" in Building Applications with JBuilder.
To connect the DataExpress components with the JdbTable, you must specify a DataSet in the dataSet property of the component.
To set the dataSet property of the JdbTable component and connect the UI component to live data,
The column headers and live data appear in the table in the Design pane.
To compile and run the application, click the Run Project button.
Clicking the Run button compiles your source code (if it has not already been compiled), and, if no errors are found, a message indicating that the source code has successfully compiled displays in the message box at the bottom of the AppBrowser, and the application UI displays in a separate window.
If there are syntax errors in the source code, the program is not automatically run. If syntax errors are found, an error pane containing error messages appears in the lower right of the AppBrowser. Double-click on the error message to locate the source of the error in the code. Errors in programming logic may not appear until run time when the application performs in unexpected ways.
In this simple application, you will probably not encounter any syntax errors because all properties were selected from drop-down lists or browsers. Syntax errors are most likely to occur when you modify the generated code, or when you add in Java code manually.
In the running application, you'll notice the following behavior:
Although the JdbNavToolBar has a Save button, this button is dimmed, and you cannot save changes to file-based data sources such as employee.txt. If this application had connected to a true database, the Save button provides a default mechanism for saving changes back to a data source. For more information on saving changes to a database, see "Saving changes back to your data source." For more information on saving changes when the data source is a text file, see "Exporting data."
Alternatively, the keyboard can be used to navigate the data in the table.
This tutorial was intended to familiarize you with the JBuilder environment and the basic requirements for developing a database application with JBuilder.