访问JavaFX TableView ObservableArrayList中的子类属性 [英] Accessing Subclass properties in a JavaFX TableView ObservableArrayList

查看:128
本文介绍了访问JavaFX TableView ObservableArrayList中的子类属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to access getter properties in a subclass with a TableView in JavaFX. I have the following class:

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 {

Person is like this: public class Person implements 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;
}
}

好的-最后,我们有以下内容:

Okay - lastly we have the following:

@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",然后在没有对象Person属性的情况下获得"firstName","lastName"等其他属性.任何帮助将不胜感激.

And I'm unsure how with a list of PersonTypes I can tell the table columns that I want the firstName and lastName properties of the Person object contained within. I know I could create a new object, and have the "count" from PersonTypes, then the other properties of "firstName", "lastName" etc without having an object property of Person. Any help would be greatly appreciated.

-编辑-

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

Another way I thought to do this was using CellFactories - where I would pass in to the CellValueFactories the Person object, then set the CellFactory to return a String value (firstName for the first name column, etc). And it would look like this:

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

推荐答案

尝试一下:

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

这篇关于访问JavaFX TableView ObservableArrayList中的子类属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆