This code is what I want to do. While typing in an editable ComboBox I want to release ENTER and handle that enter event. However, I cannot get the application to respond, a message was not printed. I wrote basically the same code for a text box and it worked fine, a message was printed. I also wrote the handler for any KeyReleased event for a ComboBox and that worked fine also, a message was printed. The trouble is the enter key. Why does this code not do what I want in an editable ComboBox?

@FXML
ComboBox comboBox;

public class ScreenController implements Initializable {

    @Override
    public void initialize(...) {
        ...
        comboBox.setOnKeyReleased(new EventHandler<KeyEvent>() {

            @Override
            public void handle(KeyEvent ke) {
                if (ke.getCode == KeyCode.ENTER) {
                    System.out.println("ENTER was released");
            }
            }
        }); 
    }
}
有帮助吗?

解决方案 2

It's look to be a JavaFX bug. setOnKeyPressed doesn't work to. look at this javafx jira

其他提示

I was suffering from the same bug/feature. Luckily I found this posting The solution is not to register your handler via comboBox.setOnKeyReleased(). instead, use EventFilter:

comboBox.addEventFilter(KeyEvent.KEY_RELEASED, new EventHandler<KeyEvent>() {
        @Override
        public void handle(KeyEvent ke) {
            if (ke.getCode == KeyCode.ENTER) {
                System.out.println("ENTER was released");
            }
        }
 });

This actually works as expected.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top