簡體   English   中英

JavaFx-獲取TableCell數據類型

[英]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的值在編譯時被刪除:本質上TObject取代, 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM