简体   繁体   English

JavaFx-获取TableCell数据类型

[英]JavaFx - Get TableCell Data Type

How do you determine the datatype (String, Integer, Double etc.) of a javafx tablecell reference. 如何确定javafx表格单元格引用的数据类型(字符串,整数,双精度数等)。 For example. 例如。 I have a custom cell factory that is assigned to a field in a Javafx controller as follows: 我有一个自定义单元工厂,它被分配给Javafx控制器中的字段,如下所示:

CustomCellFactory<Damageloop, Date> damageLoopWorkshopDueDateFactory = new CustomCellFactory ("colDamageLoopWorkshopDueDate", false);

This calls the following class... 这称为以下课程...

public class CustomCellFactory<S, T> implements Callback<TableColumn<S, T>, TableCell<S, T>>  {

    String colname;
    boolean editable;

    public CustomCellFactory (String colname, boolean editable) {
        this.colname = colname;
        this.editable = editable;
    }

    @Override
    public TableCell<S, T> call(TableColumn<S, T> arg) {
        TableCell<S, T> cell;
        if (editable == true) {
            cell = new TableCellEditable<>(colname);
        } else {
            cell = new TableCellCustom<>(colname);
        }
        return cell;
    }

}

Which in turn calls the following class... 依次调用以下课程...

public class TableCellCustom <S, T> extends TableCell<S, T> {

    String colname;

    public TableCellCustom (String colname) {
        this.colname = colname;
    }

    @Override
    protected void updateItem(T item, boolean empty) {
        super.updateItem(item, empty);
        if (empty || item == null) {
            setText(null);
            setGraphic(null);
        } else {
            setConditionalFormatting(this,item);
        }
    }

    public void setConditionalFormatting (TableCell<S,T> cell, T item) {
        //perform specific cell formatting....
    }
}

And would like to determine the datatype of the cell at the TableCellCustom level (ie the initial parameter that is passed when the CustomCellFactory is created, in this case Date) so specific operations can be performed based on this. 并希望在TableCellCustom级别确定单元的数据类型(即,创建CustomCellFactory时传递的初始参数,在这种情况下为Date),因此可以基于此执行特定操作。 I have tried... 我努力了...

super.itemProperty().getName()

And this returns a whole bunch of information associated to the cell as follows: 这将返回与该单元格相关的一堆信息,如下所示:

ObjectProperty [bean: EditableTableCell[id=colDamageLoopWorkshopDueDate, styleClass=cell indexed-cell table-cell table-column]'17/01/2016', name: item, value: 2016-01-17]

But no reference to the cell datatype. 但是没有引用单元格数据类型。

The value of the type variable T is removed at compile time: essentially T is replaced by Object and return values of T replaced by the appropriate cast. 类型变量T的值在编译时被删除:本质上TObject取代, T返回值被适当的强制转换代替。 So there is no way to find at runtime which type T stands for in a particular instance of your class. 因此,无法在运行时查找类的特定实例中T代表的类型。

So if you really need to access this information, you need to add a field to represent the type. 因此,如果您确实需要访问此信息,则需要添加一个字段来表示类型。 Ie you need: 即您需要:

public class TableCellCustom <S, T> extends TableCell<S, T> {

    // the type of T:
    private final Class<T> type ;

    String colname;

    public TableCellCustom (String colname, Class<T> type) {
        this.colname = colname;
        this.type = type ;
    }

    @Override
    protected void updateItem(T item, boolean empty) {
        super.updateItem(item, empty);
        if (empty || item == null) {
            setText(null);
            setGraphic(null);
        } else {
            setConditionalFormatting(this, item);
        }
    }

    public void setConditionalFormatting (TableCell<S,T> cell, T item) {
        //perform specific cell formatting....
        if (type == Date.class) {
            // ...
        } else {
            // ...
        }
    }
}

Of course to make this work, you also need 当然要进行这项工作,您还需要

public class CustomCellFactory<S, T> implements Callback<TableColumn<S, T>, TableCell<S, T>>  {

    String colname;
    boolean editable;

    private final Class<T> type ;

    public CustomCellFactory (String colname, boolean editable, Class<T> type) {
        this.colname = colname;
        this.editable = editable;
        this.type = type ;
    }

    @Override
    public TableCell<S, T> call(TableColumn<S, T> arg) {
        TableCell<S, T> cell;
        if (editable == true) {
            cell = new TableCellEditable<>(colname);
        } else {
            cell = new TableCellCustom<>(colname, type);
        }
        return cell;
    }

}

and then you do 然后你做

CustomCellFactory<Damageloop, Date> damageLoopWorkshopDueDateFactory = 
    new CustomCellFactory ("colDamageLoopWorkshopDueDate", false, Date.class);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM