0

I want to make an autocomplete text field with suggestions where data is from the Google API - updated after each new key pressed. For now, I have a method that downloads 5 suggestions and updates them when another key is pressed.

I've tried AutoCompleteTextField from Gluon, but it didn't work out well.

public class Controller {
    Weather weather = new Weather();
    GooglePlaces googlePlaces = new GooglePlaces();

    @FXML
    AutoCompleteTextField<String> autoCompleteTextField = new AutoCompleteTextField<>();

    @FXML
    public void setAutoComplete() throws IOException {
        ArrayList<String[]> places = googlePlaces.predictPlaces("New yo");

        autoCompleteTextField.setCompleter(s -> {
            ArrayList<String> autoplaces = new ArrayList<>();
            for (int i = 0; i < places.size(); i++) {
                autoplaces.add(places.get(i)[0]);
            }
            System.out.println("test");
            return autoplaces;
        });
    }
}

Here I tried to add 5 suggestions from "New yo" phase without updating after each new key, but it didn't work either as it doesn't show anything. "test" isnt printed in the console.

tkruse
  • 10,222
  • 7
  • 53
  • 80
Gohini
  • 69
  • 5

1 Answers1

0

Seems to me that you need to call setCompleter() in a method called initialize():

public class Controller {

    @FXML
    public void initialize() {
        autoCompleteTextField.setCompleter(input -> {
            List<String[]> places = googlePlaces.predictPlaces(input);
            // ...
        });
    }
}

Also see:

tkruse
  • 10,222
  • 7
  • 53
  • 80