static <T, S> T getObjectAtEvent(CellEditEvent<T, S> evt) { TableView<T> tableView = evt.getTableView(); ObservableList<T> items = tableView.getItems(); TablePosition<T,S> tablePosition = evt.getTablePosition(); int rowId = tablePosition.getRow(); T obj = items.get(rowId); return obj; }
This method is for working with JavaFX Controller objects like these
@FXML TableColumn<MyObject,Number> tcId; @FXML TableColumn<MyObject,String> tcData; @FXML TableView<MyObject> tblObjects;
Where MyObject is a POJO with id and data fields.
The getObjectAtEvent() method can be used in the Edit Commit handler for the TableColumn.
tcData.setCellValueFactory(new PropertyValueFactory<MyObject,String>("data")); tcData.setCellFactory(TextFieldTableCell.forTableColumn()); tcData.setOnEditCommit((evt) -> { MyObject obj = getObjectAtEvent(evt); // <--- APPLYING THE METHOD if( !dirtyFlag.get() ) { dirtyFlag.set(true); } String oldData = obj.getData(); obj.setData( evt.getNewValue() ); updateList.add( new UpdateObject(obj.getId(), obj.getData(), oldData)); });
No comments:
Post a Comment