PropertyValueFactory在模型中是否需要valueProperty方法?

时间:2014-11-07 07:39:07

标签: java javafx-8

我正在尝试使用TableView创建一个表,并根据Actors对象列表填充它。演员模型如下。

public class Actor {

    private SimpleIntegerProperty actorId;
    private SimpleStringProperty firstName;
    private SimpleStringProperty lastName;
    private SimpleStringProperty email;


    public Actor(int id, String first, String last, String e){
        actorId = new SimpleIntegerProperty(id);
        firstName = new SimpleStringProperty(first);
        lastName = new SimpleStringProperty(last);
        email = new SimpleStringProperty(e);
    }

    public void setActorId(int id){
        actorId.set(id);
    }
    public int getActorId(){
        return actorId.get();
    }

    public void setFirstName(String name){
        firstName.set(name);
    }
    public String getFirstName(){
        return firstName.get();
    }

    public void setLastName(String last){
        lastName.set(last);
    }
    public String getLastName(){
        return lastName.get();
    }

    public void setEmail(String e){
        email.set(e);
    }


    public String getEmail(){
        return email.get();
    }

}

这是我的TableVeiw类

public class SakilaApp extends Application {

    private TableView<Actor> actorTable = new TableView<Actor>();

    private final ObservableList<Actor> actorData = FXCollections.observableArrayList(
            new Actor(1, "Mohsen","Parsa", "Mohseh.parsa313@gmail.com"),
            new Actor(2, "Morteza","Ghasemi", "Morteza.Ghasemi@gmail.com"),
            new Actor(3, "Mohammad","Fetrat", "Mohammad.Fetrat@gmail.com"),
            new Actor(4, "Nader","AhmadYar", "Nader.AhmadYar@gmail.com" )
            );

    @SuppressWarnings("unchecked")
    @Override
    public void start(Stage stage) {
        Scene scene = new Scene(new Group());
        stage.setTitle("Table View Sample");
        stage.setWidth(600);
        stage.setHeight(500);
        final Label label = new Label("Address Book");
        label.setFont(new Font("Arial", 20));



        actorTable.setEditable(true);

        TableColumn<Actor, Integer> idCol = new TableColumn<Actor, Integer>("Actor ID");
        idCol.setCellValueFactory(
                new PropertyValueFactory<Actor, Integer>("actorId"));
        idCol.setPrefWidth(60);

        TableColumn<Actor, String> firstNameCol = new TableColumn<Actor, String>("First Name");
        firstNameCol.setCellValueFactory(
                new PropertyValueFactory<Actor, String>("firstName"));  
        firstNameCol.setPrefWidth(100);


        TableColumn<Actor, String> lastNameCol = new TableColumn<Actor, String>("Last Name");
        lastNameCol.setCellValueFactory(
                new PropertyValueFactory<Actor, String>("lastName"));
        lastNameCol.setPrefWidth(100);

        TableColumn<Actor, String> emailCol = new TableColumn<Actor, String>("Email");
        emailCol.setCellValueFactory(
                new PropertyValueFactory<Actor, String>("email"));
        emailCol.setPrefWidth(200);



        TableColumn<Actor, String> lastUpdateCol = new TableColumn<Actor, String>("Last Update");
        lastUpdateCol.setCellValueFactory(
                new PropertyValueFactory<Actor, String>("lastUpdate"));
        lastUpdateCol.setPrefWidth(100);

        actorTable.getColumns().addAll(idCol, firstNameCol, lastNameCol, emailCol);
        actorTable.setItems(actorData);

        final VBox actorBox = new VBox();
        actorBox.setSpacing(5);
        actorBox.setPadding(new Insets(10, 0, 0, 10));
        actorBox.getChildren().addAll(label, actorTable);

        ((Group) scene.getRoot()).getChildren().addAll(actorBox);
        stage.setScene(scene);
        stage.show();

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

我的问题是正如他们在本文中提到的那样 How to use the PropertyValueFactory correctly?

new PropertyValueFactory<Actor, Integer>("actorId")

将查找:

 Actor.actorIdProperty()

但正如您在Actor模型中所看到的,没有任何名称为

的方法
IntegerProperty actorIdProperty()

我的问题是,我们是否需要这样的方法? 如果有必要,为什么这段代码能正常工作?

2 个答案:

答案 0 :(得分:3)

取决于你的意思&#34;工作&#34; : - )

只要TableView是只读的,getter / setter就足够了:数据按预期显示。只要TableView可编辑,数据就不会自动更新。在后一种情况下,您可以选择安装自定义提交处理程序或公开允许内部魔术工作的属性。

无论如何你都有,我认为没有理由不(和在教程中跟随buggy example

答案 1 :(得分:1)

PropertyValueFactory的JavaDoc声明您需要一个名为

的字段
SimpleIntegerProperty actorIdProperty;

所以我认为你应该这样做。

但是,看看PropertyValueFactory的代码,我注意到如果属性字段不可用,它会回退到getter:

if (propertyRef.hasProperty()) {
    return propertyRef.getProperty(rowData);
} else {
    T value = propertyRef.get(rowData);
    return new ReadOnlyObjectWrapper<T>(value);
}

这就是为什么你的代码按原样运行的原因。

如果我是你,我会遵循JavaDoc并将你的字段重命名为actorIdProperty,因为你永远不知道他们什么时候可以改变实现。