简体   繁体   中英

JavaFx - Get TableCell Data Type

How do you determine the datatype (String, Integer, Double etc.) of a javafx tablecell reference. For example. I have a custom cell factory that is assigned to a field in a Javafx controller as follows:

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. 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. So there is no way to find at runtime which type T stands for in a particular instance of your class.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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