2

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 e.g. 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 e.g. 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!!

clic
  • 365
  • 1
  • 13
  • I don't expect the JFXtras control to have something special implemented for being rendered in the table. The TreeViewTable should IMHO take care of the hiding or showing of the (contents of its) cells. – tbeernot Jul 08 '15 at 14:06
  • Actually I chose the wrong control for my post - ColorPicker is not from jfxtras. But It's the same e.g. with a LocalTimeTextField control. So you mean TreeTableView is just not capable of handling controls? Or is there something else I could try ..? – clic Jul 08 '15 at 16:01
  • It can handle this just fine: the issue is you are telling all cells (even the empty ones) to use the control as its graphic. You need to specifically set the graphic only if the cell is non-empty. See answer... – James_D Jul 08 '15 at 16:48

1 Answers1

1

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;
        }
    });
James_D
  • 201,275
  • 16
  • 291
  • 322
  • ok thanks! this solved the problem with the empty cells! But still when I do some collapse/expand, the colorPickers do not appear in the correct row. – clic Jul 09 '15 at 07:51