JavaFX Combo Box with Search Autocomplete

JavaFX is a java framework for building cross-platform desktop application under the Java Platform, i have been doing JavaFX programming for the last couples of months and been feel really happy.

It's good practice to build a desktop application UI, instead of web-based UI, i mean, and the thing that it's under Java, it has lot of supporting tools and libraries. Yeah Java has been sucks on the UI, even me as I've been doing web for years, i feel really frustrated doing UI programming for the first time in Java.

It is hard to find for example a library to support a combo box with search functionality, you can find it a lot's of lots library in Javascript, but Java, i'd say none, i should implement it myself, and it's been really difficult as i don't think i have enough experiences in JavaFX.

So here's a simple implementation of JavaFX combo box with search autocomplete. Create a class name it
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.control.ComboBox;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;

public class AutoCompleteBox implements EventHandler{
    private ComboBox comboBox;
    final private ObservableList data;
    private Integer sid;

    public AutoCompleteBox(final ComboBox comboBox) {
        this.comboBox = comboBox; = comboBox.getItems();


    public AutoCompleteBox(final ComboBox comboBox, Integer sid) {
        this.comboBox = comboBox; = comboBox.getItems();
        this.sid = sid;


    private void doAutoCompleteBox() {
        this.comboBox.getEditor().focusedProperty().addListener((observable, oldValue, newValue) -> {
            if(newValue){//mean onfocus

        this.comboBox.getEditor().setOnMouseClicked(event ->{
                if(event.getClickCount() == 2){

        this.comboBox.getSelectionModel().selectedIndexProperty().addListener((observable, oldValue, newValue) -> {

        this.comboBox.setOnKeyPressed(t -> comboBox.hide());



    public void handle(KeyEvent event) {
        if ( event.getCode() == KeyCode.UP || event.getCode() == KeyCode.DOWN
                || event.getCode() == KeyCode.RIGHT || event.getCode() == KeyCode.LEFT
                || event.getCode() == KeyCode.HOME
                || event.getCode() == KeyCode.END || event.getCode() == KeyCode.TAB
        ) {

        if(event.getCode() == KeyCode.BACK_SPACE){
            String str = this.comboBox.getEditor().getText();
            if (str != null && str.length() > 0) {
                str = str.substring(0, str.length() - 1);
            if(str != null){

        if(event.getCode() == KeyCode.ENTER && comboBox.getSelectionModel().getSelectedIndex()>-1)


    private void setItems() {
        ObservableList list = FXCollections.observableArrayList();

        for (Object datum : {
            String s = this.comboBox.getEditor().getText().toLowerCase();
            if (datum.toString().toLowerCase().contains(s.toLowerCase())) {

        if(list.isEmpty()) this.comboBox.hide();


    private void moveCaret(int textLength) {

And to use it, just instantiate your combo box as a parameter, for example:

ComboBox myComboBox = new ComboBox();

//usually list of combobox items here
new AutoCompleteBox ( myComboBox );

This is screenshot for sample:


Popular posts from this blog

SQL Substring Not Start From 0

ERROR 1348 Column Password Is Not Updatable When Updating MySQL Root Password

How To Connect SSH Using PEM Certificate On Windows

How To Create Spring Boot Project Using Netbeans

Intellij Error: Spring class file has wrong version 61.0, should be 55.0