JavaFX tableView edit triggers KeyPress events

2.5k views Asked by At

I have a JavaFX application that is listening to some KeyPress events on the "primaryStage" which is the Group() node that contains most of the app's nodes. like this:

EventHandler kappa = new EventHandler<KeyEvent>(){
    public void handle(KeyEvent e){
        String kc = e.getCode().toString();
            if (kc == "ESCAPE") {
               primaryStage.close(); 
            }
            f.keyHandler(kc, med.getValue(), rateText, statusText); 
    }
};
primaryStage.addEventFilter(KeyEvent.KEY_PRESSED,kappa);` 

I, also have a TableView node which contains some data.

I would like my TableView to be editable, which is quite easy to implement, but when I try to edit a table cell, keys that I press to type text trigger the above KeyPress events.

An easy solution would be not to assign keypress events to the primaryStage, but I need it to be that way.

I tried to use setOnEditStart on the editable table cells where I would like to remove the keypress handler, then re-bind it on seOnEditCommit. I'm not sure whether this is a good strategy though. Any ideas on how to do this?

EDIT: Maybe some way of binding the keypress events to all nodes in primaryStage except for the TableView node?

1

There are 1 answers

2
Daniel Hesslow On

If I understand you correctly all you need to do is consume the event when it triggers on the TableView.

TableView myTableVeiw;
myTableView.setOnKeyPressed(e->{e.consume;});

Hope that helps :)