0
我想通过JavaFX中的TableView访问子类中的getter属性。我有以下类:访问JavaFX TableView中的子类属性ObservableArrayList
public class PersonType implements Serializable {
private static final long serialVersionUID = 1L;
Person person;
short count;
public PersonType() {
}
public PersonType(Person person, short count) {
super();
this.person = person;
this.count = count;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public short getCount() {
return count;
}
public void setCount(short count) {
this.count = count;
}
人是这样的: 公共类Person实现Serializable {
private static final long serialVersionUID = 1L;
String firstName;
String lastName;
public Person() {
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
好了 - 最后,我们有以下几点:
@FXML
private TableColumn tcFirstName;
@FXML
private TableColumn tcLastName;
@FXML
private TableView tblPersonTypes;
ArrayList<PersonType> pType = new ArrayList<PersonType>();
//Can assume that pType here has say 5 entries, the point of this
//is I'm trying to get to the firstName, lastName properties of the
//PersonType in the TableView below like the following:
tcFirstName.setCellValueFactory(new PropertyValueFactory<String,String>("firstName"));
tcLastName.setCellValueFactory(new PropertyValueFactory<String,String>("lastName"));
//Populate Table with Card Records
ObservableList<PersonType> data = FXCollections.observableArrayList(pType);
tblPersonTypes.setItems(data);
而且我我不确定如何使用PersonType列表,我可以告诉表列我想要包含Person对象的firstName和lastName属性。我知道我可以创建一个新对象,并拥有PersonType中的“count”,然后是“firstName”,“lastName”等其他属性,而不必具有Person的对象属性。任何帮助将不胜感激。
- 编辑 -
另一种方式我认为这样做是使用CellFactories - 在那里我会在传递给CellValueFactories Person对象,然后设置CellFactory返回一个字符串值(名字为先名称列等)。它应该是这样的:
tcFirstName.setCellValueFactory(new PropertyValueFactory<Person,String>("person"));
tcFirstName.setCellFactory(new Callback<TableColumn<Person,String>,TableCell<Person,String>>(){
@Override
public TableCell<Person,String> call(TableColumn<Person,String> param) {
TableCell<Person,String> cell = new TableCell<Person,String>(){
@Override
public void updateItem(String item, boolean empty) {
if(item!=null){
setGraphic(new Label(item.getFirstName()));
}
}
};
return cell;
}
});