Cancel edit in javafx tableview

Tags: ,



I am fairly new in JavaFX. I have a table having multiple columns and a refresh button in each row. I am trying to set the old value in the edited cells whenever I click Refresh button. One idea is Passing the old value through a global variable to refresh button and set it. I can get the old value But how can I set that old value? here is my code

 String old=null;

public void initialize(URL arg0, ResourceBundle arg1) {

            colName.setCellValueFactory(new PropertyValueFactory<ModelBrBuilding,String>("BranchName"));
            colName.setCellFactory(TextFieldTableCell.forTableColumn());
            colName.setOnEditCommit(
                    new EventHandler<CellEditEvent<ModelBrBuilding, String>>() {
                        @Override
                        public void handle(CellEditEvent<ModelBrBuilding, String> t) {
                     old= ((ModelBrBuilding) t.getTableView().getItems().get(
                                t.getTablePosition().getRow())
                                ).getBranchName();
                            ((ModelBrBuilding) t.getTableView().getItems().get(
                                    t.getTablePosition().getRow())
                                    ).setBranchName(t.getNewValue());
                        }
                    }
                );
colAction.setCellFactory(col -> {

                Button RefreshButton = new Button("Refresh");                                      
                hbox.getChildren().add(RefreshButton);

                TableCell<ModelBrBuilding, ModelBrBuilding> cell = new TableCell<ModelBrBuilding, ModelBrBuilding>() {
                    @Override
                    //Updating with the number of row 
                    public void updateItem(ModelBrBuilding building, boolean empty) {
                        super.updateItem(building, empty);
                        if (empty) {
                            setGraphic(null);
                        } else {
                            setGraphic(RefreshButton);
                        }
                    }

                };                                                  
                RefreshButton.setOnAction(event->{

                    //here I need to set the old value

                });                        
                return cell ;
            });

Can any one give me idea how can I do that?

Answer

Finally, I have found my answer which is working

RefreshButton.setOnAction(event -> {                
    ModelBrBuilding buildin = new ModelBrBuilding();                                                
    int i = tableBuilding.getSelectionModel().getSelectedIndex();                      
    buildin.setBranchName(old);

    tableBuilding.getItems().set(i, buildin);               
});


Source: stackoverflow