user3164187 user3164187 - 3 months ago 112
Java Question

JavaFX autocomplete ComboBox popup size

I have to create an auto fill

ComboBox
based on user input.

My code goes like this:

public class JavaFXApplication1 extends Application {

@Override
public void start(Stage primaryStage) {
ComboBox<String> combo = new ComboBox<>();
ObservableList<String> list = FXCollections.observableArrayList();
list.add("A");
list.add("AND");
list.add("ANDR");
list.add("ANDRE");
list.add("B");
list.add("BP");
list.add("BPO");
combo.setItems(list);
new AutoCompleteComboBoxListener(combo);

StackPane root = new StackPane();
root.getChildren().add(combo);

Scene scene = new Scene(root, 300, 250);

primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}
}


the
AutoCompleteComboBoxListener
is taken from this answer.

With this autocomplete works fine. I faced a problem in list size,

Run the application and click on the
ComboBox
drop down to see popup size.


  • Type ANDRE in the combo box(which will show ANDRE option in popup).Now delete all characters.(by backspace)

  • Now the populated list shrinked to one entry size with scroll bar.

  • Click on the combo box drop down again to get the full size list.



How can I make the list size according to content?

Answer

This is because the comboBox.hide(); method is not called (the drop-down list is automatically updated on show).

You can improve the listener as:

public class AutoCompleteComboBoxListener<T> implements EventHandler<KeyEvent> {

    private ComboBox<T> comboBox;
    private ObservableList<T> data;
    private boolean moveCaretToPos = false;
    private int caretPos;

    public AutoCompleteComboBoxListener(final ComboBox<T> comboBox) {
        this.comboBox = comboBox;
        data = comboBox.getItems();

        this.comboBox.setEditable(true);
        this.comboBox.setOnKeyReleased(AutoCompleteComboBoxListener.this);
    }

    @Override
    public void handle(KeyEvent event) {


        if(event.getCode() == KeyCode.UP) {
            caretPos = -1;
            moveCaret(comboBox.getEditor().getText().length());
            return;
        } else if(event.getCode() == KeyCode.DOWN) {
            if(!comboBox.isShowing())
                comboBox.show();

            caretPos = -1;
            moveCaret(comboBox.getEditor().getText().length());
            return;
        } 

        if (event.getCode() == KeyCode.RIGHT || event.getCode() == KeyCode.LEFT
                || event.isControlDown() || event.getCode() == KeyCode.HOME
                || event.getCode() == KeyCode.END || event.getCode() == KeyCode.TAB) {
            return;
        }

        comboBox.hide();

        if(event.getCode() == KeyCode.BACK_SPACE) {
            moveCaretToPos = true;
            caretPos = comboBox.getEditor().getCaretPosition();
        } else if(event.getCode() == KeyCode.DELETE) {
            moveCaretToPos = true;
            caretPos = comboBox.getEditor().getCaretPosition();
        }



        ObservableList<T> list = FXCollections.observableArrayList();
        for (int i=0; i<data.size(); i++) {
            if(data.get(i).toString().toLowerCase().startsWith(
                AutoCompleteComboBoxListener.this.comboBox
                .getEditor().getText().toLowerCase())) {
                list.add(data.get(i));
            }
        }
        String t = comboBox.getEditor().getText();

        comboBox.setItems(list);
        comboBox.getEditor().setText(t);
        if(!moveCaretToPos) {
            caretPos = -1;
        }
        moveCaret(t.length());
        if(!list.isEmpty()) {
            comboBox.show();
        }
    }

    private void moveCaret(int textLength) {
        if(caretPos == -1) 
            comboBox.getEditor().positionCaret(textLength);
        else 
            comboBox.getEditor().positionCaret(caretPos);

        moveCaretToPos = false;
    }

}

This update will ensure, that the drop-down list will be hidden and re-shown every time the search String has been changed.

Comments