[英]JavaFx - Get TableCell Data Type
如何确定javafx表格单元格引用的数据类型(字符串,整数,双精度数等)。 例如。 我有一个自定义单元工厂,它被分配给Javafx控制器中的字段,如下所示:
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时传递的初始参数,在这种情况下为Date),因此可以基于此执行特定操作。 我努力了...
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]
但是没有引用单元格数据类型。
类型变量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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.