Skip to content

Instantly share code, notes, and snippets.

@floralvikings
Last active December 30, 2023 04:41
Show Gist options
  • Save floralvikings/10290131 to your computer and use it in GitHub Desktop.
Save floralvikings/10290131 to your computer and use it in GitHub Desktop.
Simple JavaFX TextBox with AutoComplete functionality based on a supplied set.
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Side;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.CustomMenuItem;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import java.util.LinkedList;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
/**
* This class is a TextField which implements an "autocomplete" functionality, based on a supplied list of entries.
* @author Caleb Brinkman
*/
public class AutoCompleteTextField extends TextField
{
/** The existing autocomplete entries. */
private final SortedSet<String> entries;
/** The popup used to select an entry. */
private ContextMenu entriesPopup;
/** Construct a new AutoCompleteTextField. */
public AutoCompleteTextField() {
super();
entries = new TreeSet<>();
entriesPopup = new ContextMenu();
textProperty().addListener(new ChangeListener<String>()
{
@Override
public void changed(ObservableValue<? extends String> observableValue, String s, String s2) {
if (getText().length() == 0)
{
entriesPopup.hide();
} else
{
LinkedList<String> searchResult = new LinkedList<>();
searchResult.addAll(entries.subSet(getText(), getText() + Character.MAX_VALUE));
if (entries.size() > 0)
{
populatePopup(searchResult);
if (!entriesPopup.isShowing())
{
entriesPopup.show(AutoCompleteTextField.this, Side.BOTTOM, 0, 0);
}
} else
{
entriesPopup.hide();
}
}
}
});
focusedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> observableValue, Boolean aBoolean, Boolean aBoolean2) {
entriesPopup.hide();
}
});
}
/**
* Get the existing set of autocomplete entries.
* @return The existing autocomplete entries.
*/
public SortedSet<String> getEntries() { return entries; }
/**
* Populate the entry set with the given search results. Display is limited to 10 entries, for performance.
* @param searchResult The set of matching strings.
*/
private void populatePopup(List<String> searchResult) {
List<CustomMenuItem> menuItems = new LinkedList<>();
// If you'd like more entries, modify this line.
int maxEntries = 10;
int count = Math.min(searchResult.size(), maxEntries);
for (int i = 0; i < count; i++)
{
final String result = searchResult.get(i);
Label entryLabel = new Label(result);
CustomMenuItem item = new CustomMenuItem(entryLabel, true);
item.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent actionEvent) {
setText(result);
entriesPopup.hide();
}
});
menuItems.add(item);
}
entriesPopup.getItems().clear();
entriesPopup.getItems().addAll(menuItems);
}
}
@krstn420
Copy link

krstn420 commented Feb 7, 2017

How can i preselect the first entry of the context menu? so if I press "enter" it automatically uses the first entry.
I tried with:
entriesPopup.getSkin().getNode().lookup(".menu-item").requestFocus();
but i always get NPE from getNode()

@schoche
Copy link

schoche commented Sep 11, 2018

works great, thank you so much!

@schoche
Copy link

schoche commented Sep 23, 2018

If you want the program to search in the whole String and not only at the start, change the Line:
SearchResult.addAll(entries.subSet(getText(), getText() + Character.MAX_VALUE));
with:

String[] input = entries.toArray(new String[entries.size()]);
for(int i=0;i<input.length;i++){
if(input[i].contains(getText().toUpperCase())){
searchResult.add(input[i]);
}
}

i know its not really efficient to typCast the entries TreeSet to an Array every time, but i don't wanted to change a lot on the Programm.

@tomvingaas
Copy link

could you give me an example of how to implement it?

@sujitkuswain
Copy link

please give me example how to use this class with fxml TextField ?

@sv-git-hub
Copy link

sv-git-hub commented Mar 27, 2019

When I try to implement as suggested above it returns incompatible with the JavaFX TextField. Would know why this is successful for some of us but not others? The declaration is @FXML private TextField tbxSummary; I have also tried @FXML private AutoCompleteTextField tbxSummary; and edited the FXML document accordingly, but the field still doesn't work. I'm sure I'm just missing something simple but not sure exactly what.

Incompatible types

@satishpahuja
Copy link

I want to trigger function on selecting value from autocompletiontextfield. How we can achieve this?

@AlastairCooper
Copy link

AlastairCooper commented Aug 19, 2019

Beautiful work sir. Thank you for posting it.

@puumCore
Copy link

Please could you give me an example of how to implement it?

someone can help me?

Yesterday i created a application with javafx, but i used the FXML, how to add the AutoCompleteTextField on FXML?

Am using Jfoenix JFX Text field, how did you add it to your FXML?

@viniciosarodrigues
Copy link

Please could you give me an example of how to implement it?

someone can help me?
Yesterday i created a application with javafx, but i used the FXML, how to add the AutoCompleteTextField on FXML?

Am using Jfoenix JFX Text field, how did you add it to your FXML?

I did not add it to FXML. I instantiated on the controller.

@4bd4ll4h
Copy link

4bd4ll4h commented Aug 3, 2021

Please could you give me an example of how to implement it?

someone can help me?
Yesterday i created a application with javafx, but i used the FXML, how to add the AutoCompleteTextField on FXML?

Am using Jfoenix JFX Text field, how did you add it to your FXML?

I did not add it to FXML. I instantiated on the controller.

How?

@NaashNix
Copy link

When I try to implement as suggested above it returns incompatible with the JavaFX TextField. Would know why this is successful for some of us but not others? The declaration is @FXML private TextField tbxSummary; I have also tried @FXML private AutoCompleteTextField tbxSummary; and edited the FXML document accordingly, but the field still doesn't work. I'm sure I'm just missing something simple but not sure exactly what.

Incompatible types

Did you find any solution?

@Hariiharan1998
Copy link

There is an issue here, when we click on the empty space of the CustomMenuItem its not clickable, its clickable only if we click on the text in the CustomMenuItem. Any solution for this? I cannot use MenuItem only I can use CustomMenuItem because with CustomMenuItem i can highlight the entered text in the textfield, in list of customMenuItem. please suggest me some solution for this.

@nadavq
Copy link

nadavq commented May 1, 2023

Works amazing until today, i even used it from a kotlin class,
I implemented it like @EzequielAyzenberg mentioned

@GQ16
Copy link

GQ16 commented Dec 30, 2023

Thank you so much. I wasted hours trying to get the Gluon and ControlsFX AutoCompleteTextFields to work to no avail in Maven. This was so much easier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment