I modeled a TreeView<Playlist>
such that the nodes that have children, have a Folder icon as graphic, and the other ones have a playlist icon. The styles almost works, but when expanding/collapsing nodes, the graphic icons get broken.
When a node is clicked, it always shows the correct style, but when expanding/collapsing, or when new nodes are added, the TreeView
act like this:
Here is part of the code where is defined the TreeCell
in the TreeView
public class PlaylistTreeView extends TreeView<Playlist> {
public PlaylistTreeView() {
...
setShowRoot(false);
setCellFactory(treeView -> new PlaylistTreeCell());
...
}
private class PlaylistTreeCell extends TreeCell<Playlist> {
@Override
public void updateItem(Playlist p, boolean empty) {
super.updateItem(p, empty);
if(p == null || empty) {
textProperty().unbind();
setText("");
setGraphic(null);
}
else {
if(p.isFolder())
setId("playlist-folder-tree-cell");
else
setId("playlist-tree-cell");
textProperty().bind(p.nameProperty());
}
}
}
}
And these are the only styles that affect those components by css
:
.tree-cell, .list-cell {
-fx-background-color: rgb(243, 243, 243);
}
#playlist-tree-cell {
-fx-graphic: url('../icons/playlist-black-icon.png');
}
#playlist-tree-cell:selected {
-fx-graphic: url('../icons/playlist-white-icon.png');
}
#playlist-folder-tree-cell {
-fx-graphic: url('../icons/playlist-folder-black-icon.png');
}
#playlist-folder-tree-cell:selected {
-fx-graphic: url('../icons/playlist-folder-white-icon.png');
}
.list-cell:selected, .tree-cell:selected {
-fx-background-color: black;
-fx-text-fill: white;
}
What am I missing here?