问题
I'm trying to have a KeyEvent handler on a TableRow of TableView, but the handler doesn't get called. I'm aware that a handler can be registered on the TableView and use the selectionmodel/ focus model to get the slected or focussed item but i want to register a key event on row for a specific use case. The documentation of onKeyPressed event says,
Defines a function to be called when this Node or its child Node has input focus and a key has been pressed. The function is called only if the event hasn't been already consumed during its capturing or bubbling phase.
The table row doesn't get the event because some other node has already consumed it ?
Is there a way i can make the event triggered on to the tablerow? Here's the code i'm using
table.setRowFactory(tv -> {
TableRow<MyObject> row = new TableRow<>();
row.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
// TODO Auto-generated method stub
System.out.println(" In the key event");
}
});
回答1:
table.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
if(event.getCode().equals(KeyCode.ENTER)) {
MyObject rowData = (MyObject)tbl.getSelectionModel().getSelectedItem();
System.out.println(rowData.getName);
}
}
});
来源:https://stackoverflow.com/questions/36452735/onkeypressed-event-not-triggered-on-tablerow-in-tableview-javafx8