2016-12-14 98 views
0

如何确定javafx tablecell引用的数据类型(String,Integer,Double等)。例如。我有如下分配给场中一个JavaFX控制器中的自定义单元格工厂:JavaFx - 获取TableCell数据类型

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

这就需要下面的类...

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

} 

依次调用下面的类.. 。

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

,并想以确定在水平TableCellCustom的小区的数据类型(即,在创建CustomCellFactory时通过,在这种情况下的日期初始参数),以便SP基于此可以执行特殊操作。我曾尝试...

super.itemProperty().getName() 

这如下返回相关的小区信息一大堆:

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

但对细胞的数据类型没有提及。

+0

你能不能给一点在这里更多的环境?本书的答案是数据类型是'T',但我猜这不是你的意思...... –

+0

@James_D更新提供更多的上下文。而且我绝对不会在T之后! – Josh

回答

2

在编译时删除类型变量T的值:基本上T被替换为Object,返回值T被替换为适当的类型。因此,无法在运行时找到哪种类型的T代表您的班级的特定实例。

所以,如果你真的需要访问这些信息,你需要添加一个字段来表示类型。即您需要:

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 { 
      // ... 
     } 
    } 
} 

当然,使这项工作,你还需要

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

} 

,然后你做

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

伟大的综合答案。谢谢! – Josh