JavaFx TableCellFactory - 获取Object或ColumnIndex

时间:2015-03-11 17:49:37

标签: javafx

也许是一个简单的问题,但不知怎的,我无法弄明白。谢谢你的帮助。

我正在尝试使用动态字段创建动态表(动态列计数)。每个单元格应该是一个对象,不仅包含原始文本,还包含背景颜色等信息。到目前为止,这只适用于一件小事:我在cellFactory中处理它时需要相应的对象。对象项是值String,不适用于此。 getTableRow()。getItem()更好,它至少是相应的行(ObservableList<>)。有没有更好的方法来获得该项目?或者我如何获得列索引?

public class FXTableColumn {

    @SuppressWarnings("Convert2Lambda")
    public static TableColumn<ObservableList<CellValue>, Object> createColumn(
      final int columnIndex, String columnTitle) {
        TableColumn<ObservableList<CellValue>, Object> column;
        column = new TableColumn<>();
        column.setText(columnTitle);
        column.setCellValueFactory(new Callback<CellDataFeatures<ObservableList<CellValue>,Object>,ObservableValue<Object>>(){   
               public ObservableValue<Object> call(CellDataFeatures<ObservableList<CellValue>, Object> param) {                        
                        return param.getValue().get(columnIndex);                       
                    }                   

                });

        column.setCellFactory(new Callback<TableColumn<ObservableList<CellValue>, Object>,TableCell<ObservableList<CellValue>,Object>>(){
            @Override
            public TableCell<ObservableList<CellValue>,Object> call(TableColumn<ObservableList<CellValue>, Object> p) {
               return new TableCell<ObservableList<CellValue>, Object>(){
                   @Override
                   public void updateItem(Object item, boolean empty) {
                         if(item != null){

                              // so here is my problem, item is a String, not the input object

                              super.updateItem(item, empty);
                               if( getTableRow() != null ) {
                                    Object cellValue =  getTableRow().getItem();
                                    //this is better, but not perfect: this object contains now the ObservableList<CellValue>
                                    //how can I get the actual item? Or, how do I simply get the column index(which also is the index of the observableList?)

                                }
                         }  
                    }
                };
            }
        });

        return column;
    }



}

(CellValue实现ObservableValue)

0 个答案:

没有答案