I have a scene, and this fully functional button called btnRemove,
Button btnRemove = new Button("Remove");
btnRemove.setMinWidth(85);
btnRemove.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
if(mediaTable.getSelectionModel().isEmpty()){
txtNotification.setText("Please select an item from the list");
}
else{
medium.remove(mediaTable.getSelectionModel().getSelectedItem());
}
}
});
and now I want to make it so that when the DELETE-key is pressed, the btnRemove button is triggered and removes the item in focus/the selected element.
Here's the code:
scene.setOnKeyReleased(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
System.out.println(event.getCode());
if("DELETE".equals(event.getCode())) {
System.out.println("ATTEMPT ----");
btnRemove.fire();
}
}
});
When I run it, the console outputs DELETE whenever I press DELETE, but it doesn't output "ATTEMPT ----" after that.
I don't see any reason why it shouldn't trigger
What gives??
You are trying to compare a KeyCode
to a String
. Change the condition to -
if (KeyCode.DELETE == event.getCode()) { ... }
What you are seeing in the first println
is the KeyCode's toString, which apparently returns it's name.