带控件的JavaFX TreeTableView自定义TreeCell

时间:2015-07-08 13:14:16

标签: java javafx treetableview jfxtras treecell

我是JavaFX 8的新手,并试图在我的第二和第三列中使用一些jfxtras控件实现TreeTableView。因此,我使用自定义TreeCells设置这些列的单元工厂,例如像这样:

col3.setCellFactory(new Callback<TreeTableColumn<Object, String>, TreeTableCell<Object, String>>() {
        @Override
        public TreeTableCell<Object, String> call(TreeTableColumn<Object, String> param) {
            TreeTableCell<Object, String> cell = new TreeTableCell<Object, String>() {
                private ColorPicker colorPicker = new ColorPicker();
                @Override
                protected void updateItem(String t, boolean bln) {
                    super.updateItem(t, bln);               
                    setGraphic(colorPicker);                        
                }
            };
            return cell;
        }
    });

现在我可以看到ColorPickers并且可以使用它们,但是列以某种方式不会对扩展或折叠第1列的节点(它显示POJO中的字符串信息)做出反应。所以例如如果我折叠整个树,第三列仍然显示所有ColorPickers。

那么还有什么必要才能让这些列同步?#/ p>

谢谢!

1 个答案:

答案 0 :(得分:0)

When the item displayed by the cell changes, the updateItem(...) method is invoked. If the cell is empty (e.g. because the user collapsed cells above), the second parameter will be true; you need to check for this and unset the graphic.

So:

col3.setCellFactory(new Callback<TreeTableColumn<Object, String>, TreeTableCell<Object, String>>() {
        @Override
        public TreeTableCell<Object, String> call(TreeTableColumn<Object, String> param) {
            TreeTableCell<Object, String> cell = new TreeTableCell<Object, String>() {
                private ColorPicker colorPicker = new ColorPicker();
                @Override
                protected void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);  
                    if (empty) {
                        setGraphic(null);
                    } else {             
                        setGraphic(colorPicker);     
                    }                   
                }
            };
            return cell;
        }
    });