java - JavaFX TableView - 更新列表并刷新表后不更新
问题描述
我有这个界面:
//global var
static List<Foo> list = new ArrayList<>();
@Override
public void start(Stage primaryStage){
TableView<Foo> tableView = new TableView<>();
tableView.setPlaceholder(new Label("No data"));
TableColumn<Foo, Integer> colIndex = new TableColumn<>("index");
colIndex.setCellValueFactory(new PropertyValueFactory<>("index"));
tableView.getColumns().add(colIndex);
tableView.setItems(FXCollections.observableArrayList(list));
Button btn = new Button("update list");
btn.setOnMouseClicked(e -> {
Random r = new Random();
List<Foo> newList = new ArrayList<>();
newList.add(new Foo(r.nextInt()));
newList.add(new Foo(r.nextInt()));
list.clear();
list.addAll(newList);
list.forEach(e1 -> System.out.println(e1.getIndex()));
tableView.refresh();
});
VBox vbox = new VBox();
vbox.getChildren().addAll(btn, tableView);
primaryStage.setScene(new Scene(vbox));
primaryStage.show();
}
Foo 类
private int index;
public Foo(int index){
this.index = index;
}
当我按下按钮时,我想用新值刷新表格list
我的问题是表在tableView.refresh()
.
为什么表不更新以及如何强制它更新?
解决方案
推荐阅读
- android - 使用 TransitionManager.go() 更改场景时看不到动画
- firebase - firebase 的 logEvent 对参数有字符限制,我如何向谷歌分析发送更多 100 个字符?
- excel - Excel:从包含特定范围内某个值的行中获取值
- javascript - 指向另一个组件的按钮上的单击方法
- reactjs - 单击 react-router 中的链接不更改用户配置文件内容
- r - 为什么我无法将文本放入堆积条形图(使用 geom_bar 的 ggplot)中而不丢失所有数据?
- javascript - ArrayBuffer 末尾有很多 0
- r - R 中同一日期的多个数据点需要平均到一个数据点
- swift - 如何在 SwiftUI 中动态隐藏导航返回按钮
- java - 如何处理“无法转换为类”错误 - GSON JAVA