0

Can TableView commit changes to generic fields like String or Integer without extra step with setOnEditCommit?

Currently I use such structure:

@FXML
TableColumn<User, String> _tableStateUsername;

@Override
public void initialize(URL location, ResourceBundle resources) {
   _tableStateUsername.setCellFactory(TextFieldTableCell.<User, String>forTableColumn(new DefaultStringConverter()));
   _tableStateUsername.setCellValueFactory(new PropertyValueFactory<User, String>("username"));
   _tableStateUsername.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<User, String>>() {
        @Override
        public void handle(TableColumn.CellEditEvent<User, String> event) {
            event.getRowValue().setUsername(event.getNewValue());
        }
    });

and it works fine, but I feel that it can be simplified (without using SimpleString)

I tried binding Value factory to field but no luck:

_tableStateUsername.setCellValueFactory(cellData -> Bindings.select(cellData.getValue(), "username"));

Is there any way to make cell set NewValue to bound field automatically?

Stanislav Orlov
  • 234
  • 1
  • 7
  • Assuming you are following the JavaFX properties pattern, this should work automatically, without the call to `setOnEditCommit(…)`. – James_D Feb 08 '23 at 11:55
  • 2
    Perhaps related: [avoid using `PropertyValueFactory`](https://stackoverflow.com/questions/72437983/why-should-i-avoid-using-propertyvaluefactory-in-javafx-and-what-should-i-use-i) – James_D Feb 08 '23 at 11:56
  • as @James_D already noted: yes, actually that's the default behaviour (provided the data class is implemented appropriately) - if the references don't help, provide a [mcve] please .. – kleopatra Feb 08 '23 at 13:39

0 Answers0