javafx的细胞工厂 [英] Cell factory in javafx

查看:162
本文介绍了javafx的细胞工厂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JavaFx 2.0和Java 7.问题是关于JavaFX中的表视图。

I am using JavaFx 2.0 and Java 7. The question is regarding Table View in JavaFX.

以下示例代码创建firstName列并为其分配单元工厂和单元格值工厂。

The below sample code creates a firstName column and assigns cell factory and cell value factory to it.

Callback<TableColumn, TableCell> cellFactory = 
new Callback<TableColumn, TableCell>() {
    public TableCell call(TableColumn p) {
        return new EditingCell();
} };


TableColumn firstNameCol = new TableColumn("First Name");
    firstNameCol.setCellValueFactory(
        new PropertyValueFactory<Person,String>("firstName")
    );
firstNameCol.setCellFactory(cellFactory);

我的要求是我的列没有直接映射到Person对象中的单个属性,而是通过连接Person对象的一个​​或多个属性创建的自定义值。

My requirement is I have a column which doesn't directly map to a single attribute in Person object, but instead is a custom value created by concatenating one or more attributes of Person object.

考虑一个场景,我有一个名为Full Name的表列,其值为Prefix + Last Name +,+ First Name。

Consider a scenario where I have a table column named Full Name which will have values of Prefix + Last Name + "," + First Name .

1)在这种情况下,您将如何编写单元格值工厂?

1) In this scenario, how will you write the cell value factory?

firstNameCol.setCellValueFactory(
            new PropertyValueFactory<Person,String>(???????)
        );

2)你将如何写单元工厂?

2) how will you write cell factory?

在这种情况下,我们是否需要实现单元值工厂和单元工厂,还是任何一个都足够了?如果一个是足够的那个?

In this scenario do we need to implement both cell value factory and cell factory or any one is sufficient? If one is sufficient then which one?

谢谢

推荐答案

直截了当,

单元格值工厂:它类似于该相关单元格的部分行项目的toString()。

Cell Factory :它是一个渲染器来自细胞项目的细胞。如果单元格项不是节点,则setGraphic((Node)cell.item),默认行为是setText(cell.item.toString())。如果单元格应支持编辑,则设置此属性;如果需要除默认标签以外的更多图形(控件),请设置此属性。

Cell Value Factory : it is like a "toString()" of only part of the row item for that related cell.
Cell Factory : it is a renderer of the cell from the cell item. Default behavior is setText(cell.item.toString()) if the cell item is not a Node, setGraphic((Node)cell.item) otherwise. Set this property if the cell is supposed to support editing OR if you want more graphics (controls) other than default Label.

因此,对于您的方案,将单元格工厂保留为默认值就足够了(2)。以下是(1)的示例代码:

So for your scenario, leaving cell factory with default value will be sufficient (2). And here is sample code for (1):

firstAndLastNameCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Person, String>, ObservableValue<String>>() {

    @Override
    public ObservableValue<String> call(TableColumn.CellDataFeatures<Person, String> p) {
        if (p.getValue() != null) {
            return new SimpleStringProperty(p.getValue().getPrefix() + " " + p.getValue().getFirstName() + "," + p.getValue().getLastName());
        } else {
            return new SimpleStringProperty("<no name>");
        }
    }
});

这篇关于javafx的细胞工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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