JavaFX: Capture "Enter" key pressed

I need to keep a list of strings with undefined size. I figured the best way to do this would be through a combo box that will accept user input and in turn add that user input when the "Enter" key is found in the list of ComboBox items, and also let the user remove those items by clicking Delete keys.

I was hoping it would be a very simple task that would run like this:

    this.cbx.setOnKeyTyped((KeyEvent E) -> {
        switch(E.getCode()){
            case  ENTER:
                this.cbx.getItems().add(this.cbx.valueProperty().get());
                this.cbx.valueProperty().set("");
                E.consume();
                break;
            case DELETE:
                if (this.cbx.getItems().contains(
                    this.cbx.valueProperty().get()
                )) this.cbx.getItems().remove(this.cbx.valueProperty().get());
                this.cbx.valueProperty().set("");
                E.consume();
                break;
        }
    });

      

Unfortunately Enter does not raise the event. So I am wrong. I also tried with onKeyPressed

and that didn't work either. What to do to lock when Enter and Delete are pressed (he selects Shift, just fine, which is insane).

EDIT 1:

Also tried

If(E.getCode().Equals(KeyCode.ENTER)){
    ...
} else if (E.getCode().equals(KeyCode.DELETE)){
    ...
}

      

No love.

Edit 2:

Building on James_D's answer below, which takes me on the right track, to accomplish what I wanted to do, I used the following method:

    ComboBox<String> cb = new ComboBox<>();
    cb.setEditable(true);
    cb.getEditor().addEventFilter(KeyEvent.KEY_PRESSED, (KeyEvent E) -> {
        switch(E.getCode()){
            case ENTER:{
                if (cb.getItems().contains(cb.getEditor().getText()))
                    E.consume();
                else{
                    cb.getItems().add(cb.getEditor().getText());
                    cb.getEditor().clear();
                    E.consume();
                }
                break;
            }
            case DELETE:{
                if (E.isControlDown() && cb.getItems().contains(cb.getEditor().getText()))
                    cb.getItems().remove(cb.getEditor().getText());
                else if (E.isAltDown()) cb.getItems().clear();

                if (E.isControlDown() || E.isAltDown()){
                    cb.getEditor().clear();
                    E.consume();
                }
                break;
            }
        }
    });

      

+3


source to share


1 answer


Do you want to have an editable combo box that adds items to its popup when the user enters items that aren't there?

If so, try:

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class UpdatingComboBox extends Application {

    @Override
    public void start(Stage primaryStage) {
        ComboBox<String> combo = new ComboBox<>();
        combo.setEditable(true);
        combo.valueProperty().addListener((obs, oldValue, newValue) -> {
            if (newValue != null && ! combo.getItems().contains(newValue)) {
                combo.getItems().add(newValue);
            }
        });
        HBox root = new HBox(combo);
        root.setAlignment(Pos.TOP_CENTER);
        primaryStage.setScene(new Scene(root, 350, 150));
        primaryStage.show();
    }

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

      



For removal, the first thing I would like to ask is whether you really want the functionality to be described by you. Typically, users wait for hitting delete in an editable combo box to delete the next character rather than remove the entire item from the list. If you really want to do this, you need to get a little bit of your hands on and use a key listener. For some reason, adding a key listener to a combo box directly seems somewhat unpredictable; it works, however, if you add it to the textbox below the editable combo box:

    combo.getEditor().addEventFilter(KeyEvent.KEY_PRESSED, event -> {
        if (event.getCode() == KeyCode.DELETE) {
            combo.getItems().remove(combo.getValue());
            event.consume();
        }
    });

      

+4


source







All Articles