简体   繁体   English

JavaFX TreeTableView自定义TreeCell与控件

[英]JavaFX TreeTableView custom TreeCell with controls

I'm new to JavaFX 8 and tried to implement a TreeTableView with some jfxtras controls in my second and third column. 我是JavaFX 8的新手,并尝试在第二列和第三列中使用一些jfxtras控件实现TreeTableView。 Therefore I set up the cell factories of those columns with custom TreeCells eg like this: 因此,我使用自定义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;
        }
    });

Now I can see the ColorPickers and can use them, but the column somehow does not react on expanding or collapsing the nodes of column 1 (which shows String information out of POJOs). 现在,我可以看到ColorPickers并可以使用它们了,但是该列对扩展或折叠第1列的节点(从POJO中显示String信息)不起作用。 So eg if I collapse the whole tree, the third column still shows all ColorPickers. 因此,例如,如果我折叠整棵树,第三列仍显示所有ColorPickers。

So what else is necessary to get the columns 'synced'? 那么,使列“同步”还需要什么呢?

Thank you!! 谢谢!!

When the item displayed by the cell changes, the updateItem(...) method is invoked. 当单元格显示的项目更改时,将调用updateItem(...)方法。 If the cell is empty (eg because the user collapsed cells above), the second parameter will be true ; 如果单元格为空(例如,因为用户折叠了上面的单元格),则第二个参数为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;
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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