1

I've a list of 'people' displayed in a JTable and stored in a database. I'd quite like users to have the results narrowed in that table as they type into a search area.

So for example typing 'S' would narrow the displayed names to 'Susan', 'Stephen', 'Steve', which would be narrowed down to just 'Susan' as soon as you type 'Su'

I was thinking of using the DocumentListener methods .removeUpdate(e) and .insertUpdate(e) to call the method that searches the underlying database and updates the table view - but this seems pretty wasteful.

In effect I'd be creating a new database connection, and other db objects, and refreshing the table every time any character was typed. I'm not sure if this is just poor design, or something that will actively cause me problems? Is there a better way of approaching this? or should I just stick to a normal search button?

NickW
  • 1,207
  • 1
  • 12
  • 52
  • [For example](https://stackoverflow.com/questions/26031383/how-to-filter-jtable-with-respect-to-a-specific-column/26031896#26031896) and [example](https://stackoverflow.com/questions/29500713/overlay-swing-text-label-as-text-is-entered/29506154#29506154) – MadProgrammer Jun 05 '17 at 22:05
  • thanks, will definitely read those as well – NickW Jun 05 '17 at 23:20

2 Answers2

2

The decision depends on your application and user requirements. There are a lot of application that provide Autocomplete functionality that is similar to yours.

Start without any optimization and see how it performs. Then you can try different optimization steps like:

1) Add wait period in between what user typed and search request. For example knowing that user will most likely search for more specific text than just 'S', wait until they type a few more characters within second or two.

2) Run the search in different thread and then update the table from there. You can have previous search thread being canceled when making next search as the user types.

3) Keep the DB connection open while user types and close after a some defined amount of time (5 sec for example). But no too long.

tsolakp
  • 5,858
  • 1
  • 22
  • 28
2

I've a list of 'people' displayed in a JTable

If all the people are displayed in a JTable then you can use the filtering feature of the JTable to display a subset of the rows in the table.

Read the section from the Swing tutorial on Sorting and Filtering for more information and working examples.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • ah of course - i don't even have to query the database. brilliant, thank you – NickW Jun 05 '17 at 23:19
  • 1
    @NickW, don't forget to "accept" the answer by clicking on the checkmark so people know the problem has been solved. – camickr Jun 06 '17 at 01:13
  • done - oh and i feel that tutorial is about to open up my program to a world of pain RE how i delete content based on selected rows... XD. thanks though – NickW Jun 06 '17 at 15:30
  • it only breaks the tablerowsorter's ability to sort by id number (which is the how i identify the row to be deleted). probably a question for another time - but not particularly a problem as I'm planning on hiding the ID number anyway. at a guess i'd say it's because i'm using the table.getSelectedRow() to retrieve the ID column value, which is then used to delete the relevant entry in the database. interesting stuff though – NickW Jun 06 '17 at 16:07
  • @NickW, `it only breaks the tablerowsorter's ability to sort by id number ` - sorting/filtering work on the same table. However, once you start using a filter/sorter, then you need to use `convertRowIndexToModel(...)` and `convertColumnIndexToModel(...)` methods if you need to access a row/column in the TableModel. – camickr Jun 06 '17 at 16:34
  • hmm, yeah, just got the custom sorting working, thanks for those links. but it turns out the ordering issues with the ID column only happen when using the JTable's tabs at the top of each column - they are initially in ID order (i've a method that checks the model & view position), then i click the tab at the top and they seem to sort themselves by the far left digit. so 101,102,23,50,602,72 etc. so I'm starting to think it's not the conversion methods- it feels like I just have to override a method in the JTable to change how it compares the integers for that column. am looking at the API now – NickW Jun 07 '17 at 17:45
  • [looks promising](https://stackoverflow.com/questions/6592192/why-does-my-jtable-sort-an-integer-column-incorrectly) – NickW Jun 07 '17 at 17:48