JavaFx:从TableView获取新旧值 [英] JavaFx: Get the new and old values from TableView
问题描述
我在TreeMap中保存了数据,并使用ObservableList呈现了地图数据.我需要直接在TableView上编辑String值.问题是,如何更改TreeMap上的真实数据,即如何从数据列表中获取旧的和新的str值并将其放入地图键中.
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方法不起作用.我用这种方法获得了新的价值.
getOldValue method dosn't work. I get with this method just the new value.
推荐答案
在lambda表达式中使用的 CellEditEvent t
实际上具有方法getOldValue()
.它将返回特定单元格的前值.
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
});
在将新值设置为新值之前,不可能将旧值从数据列表中删除吗?
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());
});
这篇关于JavaFx:从TableView获取新旧值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!