问题描述
我正在尝试在TableView的TableRow上使用KeyEvent处理程序,但不会调用该处理程序.我知道可以在TableView上注册处理程序,并使用selectionmodel/focus模型来获取选定的或重点突出的项目,但是我想针对特定用例在行上注册一个关键事件.onKeyPressed事件的文档说,
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");
}
});
推荐答案
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);
}
}
});
这篇关于在TableView Javafx8的TableRow上未触发OnKeyPressed事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!