Acceso a las propiedades de la subclase en un JavaFX TableView ObservableArrayList

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

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.

- EDITAR -

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

preguntado el 26 de agosto de 12 a las 23:08

1 Respuestas

Prueba esto:

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

Respondido 27 ago 12, 09:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.