Locating data

The dbSwing library includes a JdbNavField component that provides locate functionality in a user-interface control. The JdbNavField includes an incremental search feature for String type columns. Its columnName property specifies the column in which to perform the locate. If not set, the locate is performed on the last column visited in a JdbTable.

If you include a JdbStatusLabel component in your application, JdbNavField prompts and messages are displayed on the status label.

The samples/DataExpress/LocatingData subdirectory of your JBuilder installation includes a finished example of an application that uses the JdbNavField under the project name LocatingData.jpr. This sample shows how to set a particular column for the locate operation as well as using a JdbComboBox component to enable the user to select the column in which to locate the value.

To create this application,

  1. Select File|Close All, and File|Close Project from the menu.
  2. Select File|New from the menu. Double-click the Application icon.
  3. Enter a name and location for the project in the Project Wizard. Click Finish.
  4. Specify the package name and class name in the Application Wizard. Click Finish.

  5. Select the Design tab to activate the UI designer.

  6. Click the Database component on the Data Express tab of the component palette, then click in the Structure pane to add the component to the application.

    Open the connection property editor for the Database component by selecting, then double-clicking the connection property ellipsis in the Inspector. Set the connection properties to the JDataStore sample employee table as follows. The Connection URL points to a specific installation location. If you have installed JBuilder to a different directory, point to the correct location for your installation.
    Property name Value
    Driver com.borland.datastore.jdbc.DataStoreDriver
    URL jdbc:borland:dslocal:/usr/local/jbuilder/samples/JDataStore/datastores/employee.jds (The employee.jds database is located under the samples directory of your JBuilder installation, which may be different on your system. You can use the Browse button to browse to this file to reduce the chance of making a typing error.)
    Username Enter your name
    Password none required

    The connection dialog includes a Test Connection button. Click this button to check that the connection properties have been correctly set. Results of the connection attempt are displayed in the status area. When the connection is successful, click OK.

  7. Add a QueryDataSet component to the designer by clicking on the QueryDataSet component on the Data Express tab and then clicking in the Structure pane.

    Select the query property of the QueryDataSet component in the Inspector, double-click its ellipsis to open the QueryDescriptor dialog, and set the following properties:
    Property name Value
    Database database1
    SQL Statement SELECT * FROM EMPLOYEE

    Click Test Query to ensure that the query is runnable. When the status area indicates Success, click OK to close the dialog.

  8. Select a DBDisposeMonitor component from the More dbSwing tab. Click in the Structure pane to add it to the application. The DBDisposeMonitor will close the JDataStore when the window is closed.

  9. Set the DBDisposeMonitor's dataAwareComponentContainer property to this.

To view the data in your application we need to add some UI components and bind them to the data set. To do this,

  1. Select contentPane(BorderLayout) in the Structure pane. In the Inspector, set its layout property to null.

  2. Select a JdbNavToolBar component from the dbSwing tab. Drop the component in the area slightly above the panel in the Design frame. JdbNavToolBar automatically attaches itself to whichever DataSet has focus, so you do not need to set its dataSet property.

    The JdbNavToolBar will enable you to move quickly through the data set when the application is running, as well as provide a default mechanism for saving changes back to your data source.

  3. Click on the JdbStatusLabel component on the dbSwing tab of the component palette. Drop the component in the area slightly below the panel in the Design pane. JdbStatusLabel automatically attaches itself to whichever DataSet has focus, so you do not need to set its dataSet property.

    Among other information, the status label displays information about the current record or current operation.

  4. Add a TableScrollPane component from the dbSwing tab to the designer. Click and drag the component to the appropriate size for this application.

    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.

  5. Drop a JdbTable component from the dbSwing tab into the TableScrollPane component. Set its dataSet property to queryDataSet1.

    You'll notice that the designer displays live data at this point.

  6. Add a JdbNavField component from the More DbSwing tab of the component palette to the Design pane. In the Inspector, set its dataSet property to queryDataSet1.

  7. Add a JdbComboBox component from the dbSwing tab of the component palette to the Design pane.

  8. Set the JdbComboBox component's items property to the column name values EMP_NO, FIRST_NAME, and LAST_NAME.

  9. Select the Events tab of the Inspector. Select the JdbComboBox component's itemStateChanged() event, and double-click its value field. A stub for the itemStateChanged() event is added to the source, and the cursor is positioned for insertion of the following code, which allows the user to specify the column in which to locate data.
    void jdbComboBox1_itemStateChanged(ItemEvent e) {
      jdbNavField1.setColumnName(jdbComboBox1.getSelectedItem().toString());
      jdbNavField1.requestFocus();
    }
    
    This code tests for the a change in the JdbComboBox. If it determines that a different column value is selected, the columnName property for the JdbNavField is set to the column named in the JdbComboBox. This instructs the JdbNavField to perform locates in the specified Column. Focus is then shifted to the JdbNavField so that you can enter the value to search for.

  10. Add a JdbTextArea component from the dbSwing tab of the component palette to the Design pane. Place it next to the JdbComboBox component. In the Inspector, set its text property so that the user knows to select a column on which to locate data, for example, Select the column in which to search. Then type the value you want to locate. Look at messages on the status label for search instructions..

  11. Add a JdbLabel component from the dbSwing tab of the component palette to the Design pane. Place it next to the JdbNavField component. In the Inspector, set its text property to: Value to locate

    .

  12. Run the application.

When you run the application, you'll notice the following behavior: