Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could I implement autocompletion using Swing?

I'm interested in providing an autocompletion box in a JFrame. The triggering mechanism will be based on mnemonics (I think), but I'm not really sure what to use for the "autocompletion box" (I would like results to be filtered as the user presses keys).

How would you implement this? Some sort of JFrame, or a JPopupMenu?

I would like to know how this is implemented, so please don't post links to available [J]Components.

like image 398
Geo Avatar asked Jan 27 '09 22:01

Geo


People also ask

How do you create an autocomplete in Java?

Using the Code add("example"); keywords. add("autocomplete"); keywords. add("stackabuse"); keywords. add("java"); Autocomplete autoComplete = new Autocomplete(mainTextField, keywords); mainTextField.

What can you use Java Swing for?

Swing in java is part of Java foundation class which is lightweight and platform independent. It is used for creating window based applications. It includes components like button, scroll bar, text field etc. Putting together all these components makes a graphical user interface.

What does pack () do in Java Swing?

The pack() method is defined in Window class in Java and it sizes the frame so that all its contents are at or above their preferred sizes. An alternative to the pack() method is to establish a frame size explicitly by calling the setSize() or setBounds() methods.


2 Answers

You might want to try the free AutoComplete component over at SwingLabs.

http://swinglabs.org

Edit: This site seems to have moved http://java.net/projects/swinglabs

There is an example how to implement this code at:

http://download.java.net/javadesktop/swinglabs/releases/0.8/docs/api/org/jdesktop/swingx/autocomplete/package-summary.html

like image 143
Geekygecko Avatar answered Sep 23 '22 04:09

Geekygecko


There is an example for auto-completion for text area at
Sun's tutorials "Using Swing Components".

It is done in the style of word processors (no pop ups, but the
suggested text is typed ahead of the cursor).

Just scroll down to "Another Example: TextAreaDemo"
ant hit the Launch button!

like image 22
ivan_ivanovich_ivanoff Avatar answered Sep 24 '22 04:09

ivan_ivanovich_ivanoff