I am would like to create element looking and behaving as the one shown below.
There is dark background and top list of 4 elements: "biblioteki", "Analiza" and so on. hen we click on one of them list is expanded and this item and its children gets light dark backgorund. Additionaly selected item from child list gets different font (bold and white). Also there could be only one item expanded at the time.
So I figure out that this is TreeView behaviour with proper styles applied. I get it working with following code:
TreeView<TabMenuElement> treeView = new TreeView<>(treeRoot);
treeView.setCellFactory(tv -> new TreeCell<TabMenuElement>() {
@Override
public void updateItem(TabMenuElement item, boolean empty) {
super.updateItem(item, empty);
setDisclosureNode(null);
if (empty) {
setText("");
setGraphic(null);
} else {
setText(item.getName()); // appropriate text for item
if (item.getIv() != null) {
setGraphic(item.getIv());
}
}
}
});
treeView.setShowRoot(false);
TabMenuElement have method getIV to obtain ImageView (icon) if it is define for this elemnt and getName to obtain text to be displayed. For now it looks like this
So I have following problems:
- how to change font only on selected item in TreeView?
- how to setup background on selected sub-tree?
- how to force that at most one subtree could be expanded
- how to set bigger size to top level items?
I figured by myself how to expand on single click treeview:
specially the part with setOnMouseClicked.
Also having the same indent for all nodes could be done in this case by setting in
Well this is for my case where icons for top level element have width 22. Then I have only one sub level and there I set the same ident. If you want to have just in whole tree the same indent without icsons it is enough to set indet to 0 for all .tree-cell.
In your css file, just define the font for
.tree-cell:selected
:This one's a little tricky. You want the background of any expanded node, and any node whose parent is not the root, to be different (that's not how you phrased it but I think it is logically equivalent). An expanded node has a CSS pseudoclass already. For the "parent is not the root", you need to define your own pseudoclass:
Now observe the
treeItem
property of the cell, and update the pseudoclass state when it changes:Now in your CSS file you can do
Add the following
ChangeListener
to each of yourTreeItem
's expanded properties:(Or change the font size, or similar.)
Here is a complete example:
with styled-unique-expanded-tree.css: