JavaFX TableView和ObservableList - 如何自动更新表? [英] JavaFX TableView and ObservableList - How to auto-update the table?

查看:1581
本文介绍了JavaFX TableView和ObservableList - 如何自动更新表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有类似的问题,并且在不同的日期,但我会在这里放一个SSCCE,并试着简单地问这个。

I know questions similar to this have been asked, and on different dates, but I'll put an SSCCE in here and try to ask this simply.

I希望能够更新数据模型,并使其上的任何视图自动更新,这样任何更新模型的调用者都不知道目前的任何视图。这是我到目前为止学习/尝试过的,没有调用 TableView.refresh()它不会更新。我缺少什么?

I would like to be able to update the data model, and have any views upon it automatically update, such that any caller updating the model is not aware of whatever views there presently are. This is what I learned/tried so far, and without calling TableView.refresh() it does not update. What am I missing?

main.java:

main.java:

package application;

import javafx.application.Application;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;

public class Main extends Application {
    @Override
    public void start(Stage stage) {

        // data
        ObservableList<Crew> data = FXCollections.observableArrayList();
        data.addAll(new Crew(1, "A"), new Crew(2, "B"));

        // table
        TableColumn<Crew, Integer> crewIdCol = new TableColumn<Crew, Integer>("Crew ID");
        crewIdCol.setCellValueFactory(new PropertyValueFactory<Crew, Integer>("crewId"));
        crewIdCol.setMinWidth(120);
        TableColumn<Crew, String> crewNameCol = new TableColumn<Crew, String>("Crew Name");
        crewNameCol.setCellValueFactory(new PropertyValueFactory<Crew, String>("crewName"));
        crewNameCol.setMinWidth(180);
        TableView<Crew> table = new TableView<Crew>(data);
        table.getColumns().addAll(crewIdCol, crewNameCol);
        table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

        // button
        Button button = new Button(" test ");
        button.setOnAction(ae -> {
            // test
            StringProperty nameProp = data.get(0).crewName();
            if(nameProp.get().equals("A")) {
                data.get(0).setCrewName("foo");
                // table.refresh();
                System.out.println("foo");
            } else {
                data.get(0).setCrewName("A");
                // table.refresh();
                System.out.println("A");
            }
        });

        VBox box = new VBox(10);
        box.setAlignment(Pos.CENTER);;
        box.getChildren().addAll(table, button); 
        Scene scene = new Scene(box);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}

Crew.java

Crew.java

package application;

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class Crew {
    private final IntegerProperty crewId = new SimpleIntegerProperty();
    private final StringProperty crewName = new SimpleStringProperty();

    Crew(int id, String name) {
        crewId.set(id);
        crewName.set(name);
    }

    public IntegerProperty crewId() { return crewId; }
    public final int getCrewId() { return crewId.get(); }
    public final void setCrewId(int id) { crewId.set(id); }

    public StringProperty crewName() { return crewName; }
    public final String getCrewName() { return crewName.get(); }
    public final void setCrewName(String name) { crewName.set(name); }

}


推荐答案

你的模型类 Crew 具有属性访问器方法的错误名称。如果不遵循推荐的方法命名方案,(有些遗留代码) PropertyValueFactory 将无法找到属性,因此无法观察它们的变化:

Your model class Crew has the "wrong" name for the property accessor methods. Without following the recommended method naming scheme, the (somewhat legacy code) PropertyValueFactory will not be able to find the properties, and thus will not be able to observe them for changes:

package application;

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class Crew {
    private final IntegerProperty crewId = new SimpleIntegerProperty();
    private final StringProperty crewName = new SimpleStringProperty();

    Crew(int id, String name) {
        crewId.set(id);
        crewName.set(name);
    }

    public IntegerProperty crewIdProperty() { return crewId; }
    public final int getCrewId() { return crewId.get(); }
    public final void setCrewId(int id) { crewId.set(id); }

    public StringProperty crewNameProperty() { return crewName; }
    public final String getCrewName() { return crewName.get(); }
    public final void setCrewName(String name) { crewName.set(name); }

}

或者,只需直接实现回调:

Alternatively, just implement the callback directly:

crewIdCol.setCellValueFactory(cellData -> cellData.getValue().crewIdProperty());

在这种情况下,编译器将确保您使用属性的现有方法名称。

in which case the compiler will ensure that you use an existing method name for the property.

这篇关于JavaFX TableView和ObservableList - 如何自动更新表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆