Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list suggestions to when typing inside the text field

Hi in my Java swing application I need to show all possible existing entries with same initial characters while user typing in a text field. Lets say user type letter 'A' in a text field which should be a country 'America' and 'Africa' would be suggestions. So that user can select one from the list. Country list resides in my database. I know how to retrieve data from DB. But I don't know how to list them and which components I should use to achieve. Can any one guide me? Or if you can provide an example, that would be great.

like image 577
nath Avatar asked Jul 13 '11 05:07

nath


3 Answers

You could use the autocomplete package from SwingX. Google for "swingx autocomplete" if you need more real life examples, but the simplest way is creating a JComboBox and calling AutoCompleteDecorator.decorate(comboBox); on it.

like image 143
Konrad Garus Avatar answered Sep 28 '22 05:09

Konrad Garus


You should try JComboBox as an autosuggest box instead of JTextField. But if you still want it to be done using JTextField then...

  1. Make a JPanel containing list of suggestion.Initially it will be not visible.
  2. Whenever user types something search for it and add results to the list in JPanel.
  3. Show that JPanel at the bottom of textfield in upper layer of frame.
  4. Implement click event on list so that when ever user clicks on it the text is copied to textfield.
like image 32
Harry Joy Avatar answered Sep 28 '22 04:09

Harry Joy


there are two classes (you are needed both for correct funcionalities), Auto Complete JTextField and AutoComplete JComboBox, excelent is that you can set if is strict (allows typing if List doesn't contains ) or not

like image 34
mKorbel Avatar answered Sep 28 '22 04:09

mKorbel