Adding person.city.name to a TableView

笑着哭i 提交于 2019-12-02 15:59:12

问题


I have a TableView and some POJOs and want to bind with a property from one of them to the TableView.

However, this property is also a POJO and this should get one property to show in the TableView.

Here's my code:

<TableView fx:id="listaDeudores" layoutX="85.0" layoutY="7.0" prefHeight="200.0" prefWidth="200.0">
  <columns>
    <TableColumn prefWidth="75.0" text="Archivo">
      <cellValueFactory>
        <PropertyValueFactory property="archivo" />
      </cellValueFactory>
      <cellFactory>
        <FormattedTableCellFactory alignment="center" />                                                
      </cellFactory>
    </TableColumn>
    <TableColumn prefWidth="299.0" text="Nombres">
      <cellValueFactory>
        <PropertyValueFactory property="nombres" />
      </cellValueFactory>
      <cellFactory>
        <FormattedTableCellFactory alignment="center" />                                                
      </cellFactory>
    </TableColumn>
    <TableColumn minWidth="0.0" prefWidth="95.0" text="Actual" >
      <cellValueFactory>
        <PropertyValueFactory property="deuda.saldo" />
      </cellValueFactory>
      <cellFactory>
        <FormattedTableCellFactory alignment="center" />                                                
      </cellFactory>
    </TableColumn>
    <TableColumn prefWidth="75.0" text="Nueva" />
    <TableColumn prefWidth="75.0" text="Parcial" />
  </columns>
 </TableView>`

And my Persona and Deuda classes:

public class Persona {
    private Integer id;
    private Integer archivo;
    private String Nombres;
    private List<Recibo> recibos;
    private Deuda deuda;
}

public class Deuda {
    private Date fecha;
    private BigDecimal saldo;
    private BigDecimal nuevo;
    private BigDecimal descuento;
}

I want to set Person.deuda.saldo in fxml file, so I used:

<TableColumn minWidth="0.0" prefWidth="95.0" text="Actual" >
  <cellValueFactory>
    <PropertyValueFactory property="deuda.saldo" />
  </cellValueFactory>
  <cellFactory>
    <FormattedTableCellFactory alignment="center" />                                                
  </cellFactory>
</TableColumn>

but this won't render anything in my TableView. How can I fix this?


回答1:


You can't use a PropertyValueFactory to do this: you need to implement the Callback directly.

Put an fx:id="actualColumn" on the table column, inject the column into your controller with

@FXML
private TableColumn<Persona, BigDecimal> actualColumn ;

and in your controller's initialize() method, do:

actualColumn.setCellValueFactory(cellData -> 
    new ReadOnlyObjectWrapper<BigDecimal>(cellData.getValue().getDeuda().getSaldo()));


来源:https://stackoverflow.com/questions/26609350/adding-person-city-name-to-a-tableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!