简体   繁体   中英

How do I restore a previous value in JavaFX TableColumn's onEditCommit?

I have a table written in JavaFX, that the user has the option to edit one of its columns. When an editing of a cell takes place, I try to rename some internal object based on the new text. If that renaming fails, I issue a notification + popup an error dialog, and then I'd like to restore the original text into that field. For some reason, this gets me into an infinite loop of error popups.

mNameColumn.setOnEditCommit(
    new EventHandler<TableColumn.CellEditEvent<StateDefinition, String>>() {
        @Override
        public void handle(TableColumn.CellEditEvent<StateDefinition, String> event) {
            try {
                someObject.rename(((StateDefinition) event.getTableView().getItems().get(event.getTablePosition().getRow())).getState(), event.getNewValue());
            } catch (MYException ex) {
                MyNotificationCollector.addNotification("Failed renaming: " + ex.getLocalizedMessage(), NotificationType.SYSTEM_WARNING);
                //popup error message code here
                // Need to revert the cell's text to the previous value here...
            }
        }
    });

Try to use

TableColumn.CellEditEvent.getOldValue()

For example:

        try {
            someObject.rename(((StateDefinition) event.getTableView().getItems()
               .get(event.getTablePosition().getRow())).getState(), 
                   event.getNewValue());

        } catch (MYException ex) {
            MyNotificationCollector.addNotification("Failed renaming: " 
                + ex.getLocalizedMessage(), NotificationType.SYSTEM_WARNING);

            // popup error message code here
            // Revert the cell's text to the previous value here...

           someObject.rename(((StateDefinition) event.getTableView().getItems()
                .get(event.getTablePosition().getRow())).getState(),
                   event.getOldValue());

            // workaround for refreshing rendered view
            event.getTableView().getColumns().get(0).setVisible(false);
            event.getTableView().getColumns().get(0).setVisible(true);
        }

The last 2 lines are oldy workarounds for refreshing tableview rendered values.

EDIT:

If the backed data model (item fields of tableview) is not changed when the exception is thrown then just update tableview rendering in catch block with:

} catch (MYException ex) {
            MyNotificationCollector.addNotification("Failed renaming: " 
                + ex.getLocalizedMessage(), NotificationType.SYSTEM_WARNING);

            // workaround for refreshing rendered view
            event.getTableView().getColumns().get(0).setVisible(false);
            event.getTableView().getColumns().get(0).setVisible(true);
        }

It's a bug in TableCell<\/a> (actually, all cells - though not noticeable for all types due to different reasons): happens if a custom commit handler rejects the edited value (or changes it in any way before writing it back to the data). The technical reason is calling updateItem with the newValue

@Override 
public void commitEdit(T newValue) {
    // ... cell book-keeping
    // ... fire editCommitEvent to allow handler to save value
    // update the item within this cell, so that it represents the new value
    updateItem(newValue, false); // <-- assumes that the handler saves the new value as-is

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM