Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
934 views
in Technique[技术] by (71.8m points)

javafx - JavaFX8 treeTableView customize collapse root item

How can I customize the collapse appearance of a tree root item? By customizing I mean setting an image and center that little triangle(/new image) inside its cell.

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The following sample customizes the graphic for the root node depending on whether the root is expanded or not (by using a binding on a graphic ImageView between the view's image and the tree item's expanded property).

The sample also centers the small triangle (the disclosure arrow) vertically with respect to the graphic image by playing with the padding settings of the arrow - you will need to adjust padding appropriately for your view as centering is not automatic.

If you want to change the shape of the disclosure arrow to something different, then see Uluk's answer to: JavaFX: Use custom Node as collapse / expand branch switch for TreeView.

shut open

CustomTree.java

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.*;
import javafx.stage.Stage;

public class CustomTree extends Application {

    @SuppressWarnings("unchecked")
    @Override
    public void start(Stage stage) throws Exception {
        Image expandedImage  = new Image(
                "http://icons.iconarchive.com/icons/pelfusion/christmas-shadow-2/64/Tree-icon.png"
        );
        Image collapsedImage = new Image(
                "http://icons.iconarchive.com/icons/pelfusion/christmas-shadow/64/Tree-icon.png"
        );

        ImageView rootGraphic = new ImageView(expandedImage);

        TreeItem<String> root = new TreeItem<>("Root Node");
        root.setGraphic(rootGraphic);
        root.getChildren().setAll(
                new TreeItem<>("Item 1"),
                new TreeItem<>("Item 2"),
                new TreeItem<>("Item 3")
        );

        rootGraphic.imageProperty().bind(Bindings
                .when(root.expandedProperty())
                    .then(expandedImage)
                    .otherwise(collapsedImage)
        );
        root.setExpanded(true);

        TreeView<String> treeView = new TreeView<>(root);
        treeView.setPrefSize(300, 200);

        Scene scene = new Scene(treeView);
        scene.getStylesheets().add(
                getClass().getResource(
                        "custom-tree.css"
                ).toExternalForm()
        );
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) { launch(args); }
}

custom-tree.css

.root {
    -fx-font-size: 30px;
}

.tree-cell > .tree-disclosure-node {
  -fx-padding: 24 6 24 8;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...