0

I'm using a treeview in a javafx application and today I added drag and drop support to reorder the elements of the treeview. To do this i had to create my own CellFactory class but my code has a bug. When the content of my treeview gets smaller the area under the treeview that should be empty still contains text that shouldn't be there.

This is what my treeview looks like before collapsing and after collapsing:

before collapsing the item

after collapsing the item

I got my inspiration for the drag and drop feature from this blogpost https://brianyoung.blog/2018/08/23/javafx-treeview-drag-drop/

Before I had my own cellFactory class I had a lambda function in the setCellFactory method of my treeview and that solution didn't have this problem.

In my CellFactory class I override the call method. I think this is where i need to somehow refresh the content of the treeview to get rid of the residue text.

public class CellFactory implements Callback<TreeView<QuizElement>, TreeCell<QuizElement>> {
...

    @Override
    public TreeCell<QuizElement> call(TreeView<QuizElement> treeView) {
        TreeCell<QuizElement> cell = new TreeCell<QuizElement>() {

            @Override
            protected void updateItem(QuizElement item, boolean empty) {
                super.updateItem(item, empty);

                if (item == null) return;

                if (empty) {
                    setText("");
                } else {
                    textProperty().bind(item.getNameProperty());
                }
            }
        };

        cell.setOnDragDetected((MouseEvent event) -> dragDetected(event, cell));
        cell.setOnDragOver((DragEvent event) -> dragOver(event, cell));
        cell.setOnDragDropped((DragEvent event) -> drop(event, cell, treeView));
        cell.setOnDragDone((DragEvent event) -> clearDropLocation());

        return cell;
    }
...

In the after collapsing picture I expected the area under the last openable element to be white.

  • Please provide a [mcve] that demonstrates the problem. – kleopatra Apr 08 '19 at 16:10
  • 1
    Try changing `if (item == null) return; if { (empty) setText(""); }` to `if (empty || item == null) { textProperty().unbind(); setText(""); }`. – Slaw Apr 08 '19 at 16:37
  • Thanks a lot, that did the trick! – μ-the-ultimate Apr 08 '19 at 16:56
  • Glad to hear it. Controls like `TreeView` are virtual which means they only render enough cells to fill the visible space. The important thing to remember is that _they reuse cell instances_. If you don't properly clear the cell when it's empty or the item is null then you get "artifacts" like what you were seeing. – Slaw Apr 08 '19 at 18:39
  • Possible duplicate of [javafx listview and treeview controls are not repainted correctly](https://stackoverflow.com/questions/26821319/javafx-listview-and-treeview-controls-are-not-repainted-correctly) – Slaw Apr 08 '19 at 18:48
  • Also related: [JavaFX : TableView inside Dialog has duplicate items](https://stackoverflow.com/questions/40218817/javafx-tableview-inside-dialog-has-duplicate-items). – Slaw Apr 08 '19 at 18:49

0 Answers0