tags:

views:

1067

answers:

6

I'm wandering myself what component is the best for displaying fast search results in swing. I want to create something like this, make a text field where user can enter some text, during his entering I'll improve in back end fast search on database, and I want to show data bellow the text box, and he will be able to browse the results and on pres enter result will be displayed in table. So my question is is there any component which already have this logic for displaying? Or is it's not, what is the best way to implement that.

This search will be something what ajax gives me on web, same logic same look and feel, if it's possible on desktop application.

+1  A: 

Use Hibernate Search.

The SwingHack (http://oreilly.com/catalog/9780596009076/) book has an example of this.

l_39217_l
+3  A: 

You will have to first attach a listener to the JTextFields Document to be notified whenever the user types in the field (or changes it).

From there, you can fire off any server-side code you need. The results of that can be used to update a listbox.

A few things to keep in mind:

  1. The code to do the search against the backend must be in another thread
  2. The code that updates the list box should update the list box's model
  3. You will need to manage all your backend search results so that you only update the listbox with the most recent result (e.g. user types 'A', backenf searches for that. Meanwhile, user has typed 'C', kicking off a backend search for 'AC'. You need to ensure the results from the 'A' search dont' make it to the listbox if the 'AC' search results are available).
davetron5000
+4  A: 

Are you looking for something like an AutoComplete component for Java Swing?

SwingX has such a component. See here for the JavaDoc. It has a lot of utility methods to do various things, i.e. auto-completing a text box from the contents of a JList.

Phill Sacre
Thanks for the tip.I downloaded the swingx classes and I tried:ArrayList nums = new ArrayList();numsadd(new StringBuffer("4000"));nums.add(new StringBuffer("5000"));autoCompleteAdaptor = new TextComponentAdaptor(numsInput,nums);where numsInput is a JTextField. I don't know what do I need to setup next. Do you happen to know where I can find a sample to get started ?
George Profenza
+1  A: 

In the interest of killing two birds with one stone: have a separate indexing thread. This will:

  1. Improve the speed of searches whenever they are executed.
  2. Improve the responsiveness of the UI since indexing is happening in a separate thread.

Of course, exactly how you perform the indexing will vary widely depending on your particular application. Here is a good place to start researching: Search Indexing. And please, ignore the reference to Web 3.0 [sic].

Ryan Delucchi
+3  A: 

I strongly, strongly recommend that you take a look at Glazed Lists - this is one of the finer open source Java libraries out there, and it makes the bulk of what you are asking about super easy.

Kevin Day
+1  A: 

It is possible of course. It is simple too. For drop down list of terms just use popup menu. This is simple. The background processing of entered text is simple too. Enjoy!

Rastislav Komara