1

I'm trying to create a textbox that appears under a JTextField with word suggestions.

For example, if the user types "d", the textbox appears under the jtextfield with a suggested word like "dog" along with any other words that start with "d".

I don't know how to do this and the solutions that I've researched have not worked.

So far, I think I need to create an array list of words to compare to what the user types and then maybe have a jcombobox positioned under the text field and set visibility to true when the user types. What should I do?

Additionally, I would like to have to it so the user can press the down/up key to select a suggested word and press enter and the jfield will be replaced with the suggested word.

That can be added later though. Right now I just need a simple solution.

Thanks to everyone who helps!

can do
  • 11
  • 3
  • SwingLabs, SwingX library has AutoComplete support, for [example](http://stackoverflow.com/questions/11928999/jtextfield-autocompletion-error/11929112#11929112) – MadProgrammer Apr 17 '15 at 05:31
  • SwingX is a third party library which you can include in your project (if you're using maven, it's easy to find, otherwise you need to [download]9https://java.net/projects/swingx/downloads/directory/releases) it). If it were easy, we wouldn't have a need for a third party library – MadProgrammer Apr 17 '15 at 06:34
  • See the linked example in my first comment for suggestions, if you still have issues, I'll do another example – MadProgrammer Apr 17 '15 at 07:47

2 Answers2

0

Something like this? :

autocomplete

Or this ?

autocomplete

Really, Google is your friend.

David Limkys
  • 4,907
  • 5
  • 26
  • 38
  • The serialVersionUID is for serialization it is not important for this code to work. The enum is there to tell you in what state you are currently, found or not found, I suggest you study the code, debug it a bit to see how it works and learn from it. – David Limkys Apr 17 '15 at 05:25
  • And link only answers and strongly discouraged, as links tend to die – MadProgrammer Apr 17 '15 at 05:27
  • And (personally), I wouldn't recommend either, as the first is to limiting (in implementing a PlainDocument) and both for trying to modify the textField from within the context of the Document mutation process, but that's me – MadProgrammer Apr 17 '15 at 05:30
-2

You can use jquery autocomplete.


We have used this with jsf. We have exposed a rest web service and used jquery autocomplete to call that web service and show the suggestion.

Like below code:

$("#mainIngredientAutoComplete").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: "../api/IngredientChoices",
                dataType: "json",
                response: ($.map(data, function(v,i){
                    return {
                        label: v.MainName,
                        value: v.MainItemID
                    };
                }))
            });
        }
    });
Community
  • 1
  • 1
Naman Gala
  • 4,670
  • 1
  • 21
  • 55