How to make a search text box (with hints, like google search)

I want to create a windowed PC application using java swing. I would like to create a text box there. When entering text in this text box, I need it to show tooltips in this text box. And the user can select the desired text from the list. Same as google search in browser. Therefore, I need two functions, the first of which is simple: filter the set of lines using the text you have already entered. But how to show them on the list?

EDIT1: I need the list to be displayed if after filtering there is something that can be shown, as well as the ability to select using the up and down keys. Same as Google search, but on PC.

+4
source share
3 answers

JComboBox is the general choice for this, AutoCompleteJComboBoxer is the one I tried.

+3
source

I suggest you add a DocumentChangeListener to your JTextField to monitor inserted / deleted / updated characters:

 JTextField textField = new JTextField; textField.addDocumentListener (new DocumentListener() { public void changedUpdate(DocumentEvent e) { } public void removeUpdate(DocumentEvent e) { } public void insertUpdate(DocumentEvent e) { } } ); 

Take a look at this tutorial .

So I need several functions, at first one is easy: filter the set of lines with the already typed text.

Now that you have the typed text, filter your set of lines. Be careful when choosing an efficient way to store and retrieve rows from your data structure. With lots of lines to sort, this might not be trivial. (I think it will be difficult with an ArrayList if you set the strings if they are quite large.)

But how to show them on the list?

I think you could use JLabel. Alternatively, you can try using JComboBox by implementing your own ComboBoxModel . I do not know if it is always possible to open the combo box.

+3
source

Well ... in the JTextField, add a listener to press the key, and then, as soon as any key is pressed, use the set to sort all related items. The option has String toChar to get each character, and then publishes entries in a JList.

Secondly, you better keep your records in XML, then tracking will be easier so that any sorting algorithm can be easily applied.

+1
source

All Articles