简体   繁体   中英

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. Therefore I set up the cell factories of those columns with custom TreeCells eg like this:

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). So eg if I collapse the whole tree, the third column still shows all 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. If the cell is empty (eg 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;
        }
    });

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