OnKeyPressed event not triggered on TableRow in TableView Javafx8

谁都会走 提交于 2019-12-11 11:32:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!