将person.city.name添加到TableView [英] Adding person.city.name to a TableView

查看:91
本文介绍了将person.city.name添加到TableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TableView和一些POJO,并希望将其中一个属性绑定到TableView。

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

但是,这个属性也是一个POJO,这个应该在TableView中显示一个属性。

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

这是我的代码:

<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>`

我的人物角色 Deuda 类:

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

我想在fxml文件中设置Person.deuda.saldo,所以我用过:

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>

但这不会在我的TableView中呈现任何内容。我该如何解决这个问题?

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

推荐答案

你不能使用 PropertyValueFactory 要做到这一点:你需要直接实现 Callback

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

放一个 fx :表格列上的id =actualColumn,将列注入您的控制器

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

@FXML
private TableColumn<Persona, BigDecimal> actualColumn ;

并且在你的控制器的 initialize()方法中,执行:

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

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

这篇关于将person.city.name添加到TableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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