TableView Javafx8中的TableRow上未触发OnKeyPressed事件

时间:2016-04-06 13:33:17

标签: tableview javafx-8 fxml tablerow onkeypress

我试图在TableView的TableRow上使用KeyEvent处理程序,但处理程序没有被调用。我知道可以在TableView上注册一个处理程序,并使用selectionmodel / focus模型来获取被选中或聚焦的项目,但我想在特定用例的行上注册一个键事件。 onKeyPressed事件的文档说,

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.

表格行没有得到该事件,因为其他一些节点已经消耗了它?

有没有办法可以让事件触发到桌面? 这是我使用

的代码
    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 个答案:

答案 0 :(得分:0)

   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);

              }
        }
    });