我正在尝试使用arrayList填充TableView。我有一个名为'personArray'的数组列表,它在一个名为Person的类中填充。此数组包含元素类型,名称,性别,年龄,种族,dob,地址,声明,伤害和防御。使用断点我可以看到在创建表之前已正确填充了数组。然后在可观察的列表'people'中转换数组列表。
然后,以下代码创建表。代码应该从可观察列表'people'中获取值并填充表,但事实并非如此。该表仍为空白。我尝试了许多不同的解决方案,但没有任何效果。
TableView<Person> peopleTable = new TableView<Person>();
ObservableList<Person> people = FXCollections.observableArrayList(personArray);
TableColumn<Person, String> typeCol = new TableColumn<Person, String>("Type");
typeCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("type")
);
TableColumn<Person, String> nameCol = new TableColumn<Person, String>("Name");
nameCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("name"));
TableColumn<Person, String> genderCol = new TableColumn<Person, String>("Gender");
genderCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("gender")
);
TableColumn<Person, String> ageCol = new TableColumn<Person, String>("Age");
ageCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("age")
);
TableColumn<Person, String> ethnicityCol = new TableColumn<Person, String>("Ethnicity");
ethnicityCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("ethnicity")
);
TableColumn<Person, String> dobCol = new TableColumn<Person, String>("Date of Birth");
dobCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("dob")
);
TableColumn<Person, String> addressCol = new TableColumn<Person, String>("Address");
addressCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("address")
);
TableColumn<Person, String> statementCol = new TableColumn<Person, String>("Statement");
statementCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("statement")
);
TableColumn<Person, String> harmCol = new TableColumn<Person, String>("Harm");
harmCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("harm")
);
TableColumn<Person, String> defenceCol = new TableColumn<Person, String>("Defence");
defenceCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("defence")
);
peopleTable.getColumns().addAll(typeCol, nameCol, genderCol, ageCol, ethnicityCol, dobCol, addressCol,
statementCol, harmCol, defenceCol);
peopleTable.setItems(people);
我是Java的新手,所以我不知道该怎么做?
编辑:
这是我的Person类(不包括创建新人的create方法,并将其放在personArray中,因为它可以正常工作)。
public class Person {
protected static ArrayList<Person> personArray = new ArrayList<Person>();
protected enum personType {
Suspect("Suspect"), Victim("Victim"), Witness("Witness");
private String typeName;
personType(String typeName) {
this.typeName = typeName;
}
public String toString() {
return this.typeName;
}
}
protected enum genderType {
Male("Male"), Female("Female");
private String genderName;
genderType(String genderName) {
this.genderName = genderName;
}
public String toString() {
return this.genderName;
}
}
protected enum ethnicityType {
White("White"), Black("Black"), Asian("Asian"), Indian("Indian");
private String ethnicityName;
ethnicityType(String ethnicityName) {
this.ethnicityName = ethnicityName;
}
public String toString() {
return this.ethnicityName;
}
}
protected personType type;
protected String name;
protected genderType gender;
protected int age;
protected ethnicityType ethnicity;
protected LocalDate dob;
protected String address;
protected String statement;
protected String harm;
protected String defence;
protected Person(personType getType, String getName, genderType getGender, int getAge,
ethnicityType getEthnicity, LocalDate getDob, String getAddress, String getStatement, String getHarm, String getDefence) {
type = getType;
name = getName;
age = getAge;
gender = getGender;
ethnicity = getEthnicity;
dob = getDob;
address = getAddress;
statement = getStatement;
harm = getHarm;
defence = getDefence;
}
}
这是对的吗?因为它仍然不起作用