Domanda

I am using Java 8's TextFieldTableCell class to make a TableColumn editable.

When a particular cell on the table gets clicked, the cell becomes a `TextField and I am able to edit the text. When editing is complete i.e if enter key is pressed

col6.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<User, String>>() {

    @Override
    public void handle(CellEditEvent<User, String> event) {
        User newUser = event.getRowValue();

        log.debug("Data Changed in the column need to call DB >>new" +event.getNewValue() + "old -->   " + event.getOldValue());

        try {
            um.updateUser(newUser);
            log.debug("View updated as DB updated");
        } catch (Exception e) {
            log.error("User update failed ");
        }
    }
});

the setOnEditCommit method is called. This works as intended.

However when the updateUser() fails, it throws an exception. When this happens, I would like to have the Table's CellValue reverted to the earlier value.

The problem is that the UI continues to display the recently typed value (even though the underlying object is still holding the old value).

For the UI to revert back to the old value i.e the value in the Bean, I have to either sort the table column or perform some action so that the view gets refreshed.

I tried my:

myEditableTableCell.updateTableColumn(col6); 

in the catch block but that doesn't make any difference as the table is still holding the oldValue.

How do I revert the value of the TextfieldTableCell to that of the TableCell when an exception occurs as explained?

È stato utile?

Soluzione 2

Thanks for your reply, I was having trouble figuring out a way to call the cancelEdit method from my UI Class. Finally managed to do it. Here is the working code snippet:

col6.setCellFactory(new Callback<TableColumn<User, String>, TableCell<User, String>>() {

    @Override
    public TableCell<User, String> call(TableColumn<User, String> param) {
        TextFieldTableCell<User, String>myEditableTableCell = new TextFieldTableCell<User, String>(new DefaultStringConverter()) {
            @Override
            public void commitEdit(String val) {
                int index = this.getTableRow().getIndex();
                User newUser = this.getTableView().getItems().get(index);
                StringProperty oldval = newUser.fullNameProperty();

                try {
                    newUser.setfullNameProperty(new SimpleStringProperty(val));
                    um.updateUser(newUser);
                    log.debug("View updated as DB updated");
                    super.commitEdit(val);
                } catch (SQLException e) {
                    cancelEdit();
                    newUser.setfullNameProperty(oldval);
                    log.error("User update failed ");
                }
            }
        };

        return myEditableTableCell; 
    }
});

Altri suggerimenti

Try catching the exception on editCommit itself instead of OnEditCommit event handler. In your myEditableTableCell, try

@Override
public void commitEdit(String val) {
    try {
        super.commitEdit(val);
    } catch (Exception e) {
        log.error("Failed: ", e);
        cancelEdit();
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top