This tutorial shows how to use a data set's RowFilterListener to view only rows that meet the filter criteria. In this example, we create a JdbTextField that lets the user specify the column to filter. Then we create another JdbTextField that lets the user specify the value that must be in that column in order for the record to be displayed in the view. We add a JButton to let the user determine when to apply the filter criteria and show only those rows whose specified column contains exactly the specified value.
In this tutorial, we use a QueryDataSet component connected to a Database component to fetch data, but filtering can be done on any DataSet component.
The finished example is available as a completed project in the samples/DataExpress/FilterRows subdirectory of your JBuilder installation.
To create this 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 fileto 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.
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.
To view the data in your application we need to add some UI components and bind them to the data set. To do this,
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.
Among other information, the status label 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.
You'll notice that the designer displays live data at this point.
import com.borland.dx.text.VariantFormatter;
Variant v = new Variant(); String columnName = "Last_Name"; String columnValue = "Young"; VariantFormatter formatter;
To create the RowFilterListener as an event adapter using the visual design tools,
A RowFilterListener is automatically generated at the end of your file. It calls a new method in your class, called queryDataSet1_filterRow method.
void queryDataSet1_filterRow(ReadRow row, RowFilterResponse response) { try { if (formatter == null || columnName == null || columnValue == null || columnName.length() == 0 || columnValue.length() == 0) // user set field(s) are blank, so add all rows response.add(); else { row.getVariant(columnName, v); // fetches row's value of column // formats this to a string String s = formatter.format(v); // true means show this row if (columnValue.equals(s)) response.add(); else response.ignore(); } catch (Exception e) { System.err.println("Filter example failed"); } }
The Source tab displays the stub for the jButton1_actionPerformed method. The following code uses the adapter class to do the actual filtering of data by detaching and re-attaching the rowFilterListener event adapter that was generated in the previous step.
void jButton1_actionPerformed(ActionEvent e) { try { // Get new values for variables that the filter uses. // Then force the data set to be refiltered. columnName = jdbTextField1.getText(); columnValue = jdbTextField2.getText(); Column column = queryDataSet1.getColumn(columnName); formatter = column.getFormatter(); // Trigger a recalc of the filters queryDataSet1.refilter(); // The table should now repaint only those rows matching these criteria } catch (Exception ex) { System.err.println("Filter example failed"); } }
Leaving either the column name or the value blank removes any filtering and allows all values to be viewed.