JavaFx: Get the new and old values from TableView

♀尐吖头ヾ 提交于 2019-12-13 01:23:18

问题


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.


回答1:


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());

              });


来源:https://stackoverflow.com/questions/27454911/javafx-get-the-new-and-old-values-from-tableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!