Search |
||
Using Beans Binding to Search in a TablePosted by pkeegan on June 5, 2008 at 10:42 AM PDT
Now and again someone will ask me how you can search records in a desktop database app. Here's a reasonably simple way to do so, using mechanisms that exist in Swing and the Beans Binding library. We will create a binding between the rowSorter property of the master table in the example in my previous entries and a text field that I've just added for the search string. For this binding we will need a binding converter so that the table knows how to respond to the search string. To follow along, you can either continue with the project created in previous entries or begin with a new NetBeans project (Java Desktop Application project template) that connects to a database. Let's get started. First of all, we'll add a label and a text field for the search field as shown below.
Now we will add a converter class to the project.
We'll use this converter when we create the binding. To create the binding:
Now when you run the application, you should be able to type in the Search Filter field and see that the list of rows is reduced to only rows that contain text matching what you have typed.
Note: If you have been following this whole series of posts, you will need to make a few changes to get the New Record and Edit Record buttons to work correctly. The Edit Record button doesn't work correctly because the number of the record to display is determined according to the records displayed but applied to the whole list of records in the database. In other words, if you select the first record in a filtered list, the first record of the whole database appears for editing in the dialog.
The New Record button fails with an exception because the code to select the new row is determined according to number of records in the database table, not according to the number of records currently displayed in the table. To fix the first problem, replace the line: ec.setCurrentRecord(list.get(masterTable.getSelectedRow())); with: ec.setCurrentRecord(list.get(masterTable.convertRowIndexToModel(masterTable.getSelectedRow()))); To fix the second problem, replace the line: int row = list.size() - 1; with: int row = masterTable.getRowCount() - 1; »
Related Topics >>
Netbeans Comments
Comments are listed in date ascending order (oldest first)
Submitted by meet_simon on Thu, 2008-06-19 23:45.
You could consider using the java.util.regex.Pattern.CASE_INSENSITIVE flag to switch regex modes.
|
||
|
|
have a nice time in Prague. i'm sure you already tasted legendary Czech bier! :-) thanks again