tmn
tmn

Reputation: 11559

Intercepting setValueAt() equivalent in JavaFX TableView

I'm trying to transition to using JavaFX and off of Swing. I like the tables a lot and the great simplicity they offer (as well as the potential to create great builders and wrappers with lambdas). However, with the exception of ObservableList and some GUI behavioral applications, I'm not exactly thrilled with the data binding. I'm a big fan of immutability and my applications usually write all edits to a database and re-build the objects after re-pulling the edited data, rather than editing the objects directly.

Following Oracle's documentation, I leveraged the ReadOnlyObjectWrapper which works great. I even built some factory methods around it.

 firstNameCol.setCellValueFactory(new Callback<CellDataFeatures<Person, String>, ObservableValue<String>>() {
     public ObservableValue<String> call(CellDataFeatures<Person, String> p) {
         return new ReadOnlyObjectWrapper(p.getValue().getFirstName());
     }
  });

However, what if I want to make it editable without using data binding? What if I want to intercept a new edited value and write it to a database rather than using a binded property? I guess what I'm getting at is there an old-school setValueAt() method that JTable's had?

Upvotes: 0

Views: 261

Answers (1)

0xFF
0xFF

Reputation: 585

You can use this guide for editing and updating data (part "Editing Data in the Table").

On another way you can try use your own Cell Factory like here. In my opinion this method is more flexible because I can update not only model if I need. Also I can customize input cell (it can be TextField or TextArea, etc.)

Upvotes: 1

Related Questions