Simon Bauer
Simon Bauer

Reputation: 37

Custom CellFactory adds new nodes

i have a weird bug and I cant find the appropriate solution. I wanted to add a TreeTableView to my application that shows a task taxonomy. Since the user is able to add tasks himself but these task have to be distinguishable from tasks already exsting I want them to be yellow in my TreeView. Therefore I added a custom CellFactory:

public class TaskLibrary extends AnchorPane {
    @FXML
    private TreeTableColumn<Task,String> mainColumn;
    @FXML
    private TreeTableView<Task> taskTreeTableView;

    public TaskLibrary(){
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(getClass().getResource("../layout/TaskLibrary.fxml"));
            loader.setRoot(this);
            loader.setController(this);
            loader.load();
            mainColumn.setCellValueFactory(param -> param.getValue().getValue().taskNameProperty());
            mainColumn.setCellFactory(param -> new TreeTableCell<Task,String>(){
                @Override
                protected void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
                    Task listObject = this.getTreeTableRow().getItem();
                    if (listObject != null) {
                        if (item == null || empty) {
                            setText("");
                            setStyle("");
                        } else {
                            if (listObject.getIsDummy()) {
                                setStyle("-fx-background-color: yellow");
                            }
                            setText(listObject.getTaskName());
                        }
                    }
                }
            });

        }
        catch (IOException e){
            throw new RuntimeException(e);
        }
    }

    @FXML
    public void addNewTask(){
    }
    public void setColumnText(String text){
        mainColumn.setText(text);
    }

    public void enableDragAndDrop(){
        TaskTreeRowFactory fac = new TaskTreeRowFactory();
        taskTreeTableView.setRowFactory(fac::internalMoveFactory);
    }

    public TreeTableColumn<Task, String> getMainColumn() {
        return mainColumn;
    }

    public void setMainColumn(TreeTableColumn<Task, String> mainColumn) {
        this.mainColumn = mainColumn;
    }

    public TreeTableView<Task> getTaskTreeTableView() {
        return taskTreeTableView;
    }

    public void setTaskTreeTableView(TreeTableView<Task> taskTreeTableView) {
        this.taskTreeTableView = taskTreeTableView;
    }
}

Now there is this weird bug that expanding the last node in my treeview results in a new node:

enter image description here

is there something i missed?

Upvotes: 0

Views: 27

Answers (1)

fabian
fabian

Reputation: 82461

You only manipulate the cell, if the row's item is not null. However if the row becomes empty the cell becomes empty too but this.getTreeTableRow().getItem() yields null and you don't modify the cell to look empty. You need to always clear the text/style when the cell becomes empty:

if (empty || item == null || listObject == null) {
    setText("");
    setStyle("");
} else {
    setStyle(listObject.getIsDummy() ? "-fx-background-color: yellow" : "");
    setText(listObject.getTaskName());
}

Upvotes: 1

Related Questions