我正在尝试使用 arrayList 填充 TableView。我有一个名为“personArray”的数组列表,它填充在名为 Person 的类中。该数组包含元素类型、姓名、性别、年龄、种族、出生日期、地址、声明、伤害和防御。使用断点我可以看到在创建表之前数组已正确填充。然后将数组列表转换为可观察列表“人员”。
下面的代码然后创建表。代码应该从可观察列表“人”中获取值并填充表,但它没有。该表保持空白。我尝试了许多不同的解决方案,但没有任何效果。
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 类(不包括创建新人员并将其放置在 personArray 中的 create 方法,因为它可以正常工作)。
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;
}
}
这个对吗?因为还是不行