JavaFX 2: Save edit in TableCell

前端 未结 2 2028
隐瞒了意图╮
隐瞒了意图╮ 2021-01-15 08:54

In my JavaFX-program I use a TableCell where you can edit a value. Like shown at the examples on the JavaFX-page \"Example\", I use this function to save the changes (functi

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-15 09:11

    Listening to a change in focus on the TextField is one way.. I added a listener to the focusedProperty of the textField. The example from Oracle didn't include this. [edit - here is a link to another question that has a different approach UITableView - Better Editing through Binding? ]

    private void createTextField() {
            textField = new TextField(getItem());
            textField.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2);
    
            // Detect a change in focus on the text field.. If we lose the focus we take appropriate action
            textField.focusedProperty().addListener(new ChangeListener() {
                public void changed(ObservableValue observable, Boolean oldValue, Boolean newValue) {
                    if(!newValue.booleanValue())
                        commitEdit(textField.getText());
                }
            } );
            textField.setOnKeyReleased(new EventHandler() {
                @Override public void handle(KeyEvent t) {
                    if (t.getCode() == KeyCode.ENTER) {
                        commitEdit(textField.getText());
                    } else if (t.getCode() == KeyCode.ESCAPE) {
                        cancelEdit();
                    }
                }
            });
        }
    

提交回复
热议问题