Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaFX TableView-更新列表并刷新表后不更新_Java_Javafx - Fatal编程技术网

JavaFX TableView-更新列表并刷新表后不更新

JavaFX TableView-更新列表并刷新表后不更新,java,javafx,Java,Javafx,我有这个界面: //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")); Tabl

我有这个界面:

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

}
当我按下on按钮时,我想用
列表中的新值刷新表

我的问题是,在
tableView.refresh()之后,接口中的表没有更新


为什么表不更新以及如何强制更新?

复制给定的
集合
,这意味着您的
列表
与表的项目列表断开连接。@Slaw ye,我的坏消息<代码>可观察列表
已解决问题。谢谢无论如何,使用支持列表是个坏主意。直接修改
可观察列表
,无需调用
刷新
tableView.getItems().setAll(newList)
还请注意,最好使用
onAction
而不是
onMouseClicked
,因为这也可以使用键盘触发,并且在单击鼠标按钮(主按钮除外)时也不会触发。。。
private int index;

public Foo(int index){
    this.index = index;
}