Reputation: 57
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");
}
}
});
}
}
Upvotes: 1
Views: 1060
Reputation: 186
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.
Upvotes: 2
Reputation: 5032
It's look to be a JavaFX bug. setOnKeyPressed doesn't work to. look at this javafx jira
Upvotes: 1