访问JavaFX TableView ObservableArrayList中的子类属性

时间:2012-08-26 22:24:39

标签: tableview observablecollection javafx javafx-2 tablecolumn

我试图在JavaFX中使用TableView访问子类中的getter属性。我有以下课程:

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;
}

人是这样的:     public class 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);

我不确定如何使用PersonTypes列表告诉表列我想要包含的Person对象的firstName和lastName属性。我知道我可以创建一个新对象,并从PersonTypes获得“count”,然后是“firstName”,“lastName”等的其他属性,而不具有Person的对象属性。任何帮助将不胜感激。

- 编辑 -

我认为这样做的另一种方法是使用CellFactories - 我将传递给Cell对象的CellValueFactories,然后设置CellFactory以返回String值(firstName为first name列等)。它看起来像这样:

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;
    }   
});

1 个答案:

答案 0 :(得分:0)

试试这个:

tcFirstName.setCellValueFactory(new Callback<CellDataFeatures<PersonType, String>, ObservableValue<String>>() {
     public ObservableValue<String> call(CellDataFeatures<PersonType, String> p) {
         // p.getValue() returns the PersonType instance for a particular TableView row
         if (p.getValue() != null && p.getValue().getPerson() != null) {
              return new SimpleStringProperty(p.getValue().getPerson().getFirstName());
         } else {
             return new SimpleStringProperty("<No TC firstname>");
         }
     }
  });
 }