【问题标题】:Accessing Subclass properties in a JavaFX TableView ObservableArrayList访问 JavaFX TableView ObservableArrayList 中的子类属性
【发布时间】:2012-08-21 11:50:17
【问题描述】:

我正在尝试使用 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;
}

人是这样的: 公共类 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 获得“计数”,然后是“firstName”、“lastName”等的其他属性,而无需 Person 的对象属性。任何帮助将不胜感激。

-- 编辑--

我认为执行此操作的另一种方法是使用 CellFactories - 我将在其中将 Person 对象传递给 CellValueFactories,然后将 CellFactory 设置为返回一个字符串值(名字列的 firstName 等)。它看起来像这样:

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

【问题讨论】:

    标签: tableview observablecollection javafx javafx-2 tablecolumn


    【解决方案1】:

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 2019-12-21
      • 2018-10-17
      • 1970-01-01
      • 1970-01-01
      • 2012-06-19
      • 1970-01-01
      • 1970-01-01
      • 2013-03-24
      相关资源
      最近更新 更多