Skip to content

Commit

Permalink
Add example for suggestion select listener
Browse files Browse the repository at this point in the history
  • Loading branch information
wbadam authored Apr 11, 2017
1 parent bd84e78 commit a73f118
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,19 @@ Any Java object can represent a suggestion item and converters provide a caption
```Java
TextField planetField = new TextField();

// Apply extension and set suggestion generator
AutocompleteExtension<String> planetExtension = new AutocompleteExtension<>(
planetField);
planetExtension.setSuggestionGenerator(this::suggestPlanet);

// Notify when suggestion is selected
planetExtension.addSuggestionSelectListener(event -> {
event.getSelectedItem().ifPresent(Notification::show);
});

// ...

// Suggestion generator function, returns a list of suggestions for a user query
private List<String> suggestPlanet(String query, int cap) {
return DataSource.getPlanets().stream().filter(p -> p.contains(query))
.limit(cap).collect(Collectors.toList());
Expand Down

0 comments on commit a73f118

Please sign in to comment.