Sam Joos
Sam Joos

Reputation: 450

JavaFx: Get the new and old values from TableView

I have the data in a TreeMap and I use ObservableList to render the map data. I need to edit the String value direct on my TableView. The problem is, how can I change the real data on TreeMap, i.e. how can I get the old and new str values from data list to put it in map key.

private Map<String, Long> map = new TreeMap<>();

private ObservableList<TableBean> data = FXCollections.observableArrayList();
....

articles.setCellFactory(TextFieldTableCell.forTableColumn());
    articles.setOnEditCommit(
              t -> {
                    ((TableBean) t.getTableView().getItems().get(
                                t.getTablePosition().getRow())
                      ).setArticles(t.getNewValue());

                    //Edited:
                     System.out.println(t.getOldValue());

                  });

getOldValue method dosn't work. I get with this method just the new value.

Upvotes: 1

Views: 1020

Answers (1)

gfkri
gfkri

Reputation: 2179

The CellEditEvent t you are using within you lambda expression actually has the method getOldValue(). It will return the former value of the particular cell.

articles.setOnEditCommit(
    t -> {
        ((TableBean) t.getTableView().getItems().get(
        t.getTablePosition().getRow())
        ).setArticles(t.getNewValue());
//  you can use "t.getOldValue()" here to get the old value of the particular cell
    });

EDIT:

Would it not be possible, to get you old value out of the data list, before you set it to the new one?

articles.setCellFactory(TextFieldTableCell.forTableColumn());
articles.setOnEditCommit(
          t -> {
                // get your old value before update it
                System.out.println(((TableBean) t.getTableView().getItems().get(
                            t.getTablePosition().getRow())
                  ).getArticles());

                ((TableBean) t.getTableView().getItems().get(
                            t.getTablePosition().getRow())
                  ).setArticles(t.getNewValue());

                //Edited:
                 System.out.println(t.getOldValue());

              });

Upvotes: 0

Related Questions