Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
929 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 Answer

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...