gpt4 book ai didi

java - PropertyValueFactory 是否需要模型中的 valueProperty 方法?

转载 作者:行者123 更新时间:2023-11-29 03:15:43 26 4
gpt4 key购买 nike

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

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

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

最佳答案

取决于你所说的“工作”是什么意思:-)

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

无论如何你都有它们,我认为没有理由不这样做(并且遵循教程中的buggy example)

关于java - PropertyValueFactory 是否需要模型中的 valueProperty 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26796351/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com