JavaFX:如何在 TreeView 中添加复选框和按钮?

标签 java javafx

我是 JavaFX 的新手。

我试图在根目录下的树形表的每一列中包含一个复选框、一个标签和一个按钮。

这有可能吗?帮助我解决方案和一些相同的引用代码。

谢谢, 维韦克。

最佳答案

您需要做的是为 TreeView 设置单元工厂。

快速回复您的评论: 只是为了明确术语。树结构(在 TreeView 中实现)由节点组成。一个节点可以有多个子节点和一个父节点。没有任何父节点的节点是根节点。没有任何 child 的节点是叶子。一棵树只能有一个根节点。

但是您可以在 TreeView 中做的是隐藏根节点。如果在根节点上添加几个子节点,然后隐藏根节点,看起来就像有多个根节点。 TreeView 中的任何节点(包括根节点)都是一个 TreeItem,您始终可以通过添加一个 TreeItem 添加一个子节点>TreeItem,然后这个 child 可以拥有自己的 child ,等等。因此您可以根据需要添加任意数量的不同级别的节点。

这是一个 MCVE .

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class MCVE extends Application {

    public void start(Stage stage) {

        VBox view = new VBox();
        view.setPrefSize(600, 400);

        // Creating the root node
        final TreeItem<String> root = new TreeItem<>("Root node");
        root.setExpanded(true);

        // Creating the tree items that will be the first children of the root node
        // and the parent to the child nodes.
        final TreeItem<String> parentNode1 = new TreeItem<>("Parent node 1");
        final TreeItem<String> parentNode2 = new TreeItem<>("Parent node 2");
        final TreeItem<String> parentNode3 = new TreeItem<>("Parent node 3");

        // Creating the tree items that will be the children of the parent
        // nodes.
        final TreeItem<String> childNode1 = new TreeItem<>("Child Node 1");
        final TreeItem<String> childNode2 = new TreeItem<>("Child Node 2");
        final TreeItem<String> childNode3 = new TreeItem<>("Child Node 3");

        // Adding tree items to the root
        root.getChildren().setAll(parentNode1, parentNode2, parentNode3);

        // Add the child nodes to all children of the root
        for (TreeItem<String> parent : root.getChildren()) {
            parent.getChildren().addAll(childNode1, childNode2, childNode3);
        }

        // Creating a tree table view
        final TreeView<String> treeView = new TreeView<>(root);

        // We set show root to false. This will hide the root and only show it's children in the treeview.
        treeView.setShowRoot(false);

        treeView.setCellFactory(e -> new CustomCell());

        view.getChildren().add(treeView);

        Scene scene = new Scene(view);
        stage.setScene(scene);
        stage.show();
    }

    /**
     * A custom cell that shows a checkbox, label and button in the
     * TreeCell.
     */
    class CustomCell extends TreeCell<String> {
        @Override
        protected void updateItem(String item, boolean empty) {
            super.updateItem(item, empty);

            // If the cell is empty we don't show anything.
            if (isEmpty()) {
                setGraphic(null);
                setText(null);
            } else {
                // We only show the custom cell if it is a leaf, meaning it has
                // no children.
                if (this.getTreeItem().isLeaf()) {

                    // A custom HBox that will contain your check box, label and
                    // button.
                    HBox cellBox = new HBox(10);

                    CheckBox checkBox = new CheckBox();
                    Label label = new Label(item);
                    Button button = new Button("Press!");
                    // Here we bind the pref height of the label to the height of the checkbox. This way the label and the checkbox will have the same size. 
                    label.prefHeightProperty().bind(checkBox.heightProperty());

                    cellBox.getChildren().addAll(checkBox, label, button);

                    // We set the cellBox as the graphic of the cell.
                    setGraphic(cellBox);
                    setText(null);
                } else {
                    // If this is the root we just display the text.
                    setGraphic(null);
                    setText(item);
                }
            }
        }
    }

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

另见 documentation Cell 类,介绍如何提供自定义单元工厂。

如果有任何不清楚的地方,请告诉我!

关于JavaFX:如何在 TreeView 中添加复选框和按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33360921/

相关文章:

java.lang.Integer.parseInt 失败,十六进制字符串使用最高位

Java 迭代器无限循环

java - JBoss RoleMappingLoginModule 未加载角色属性

css - JavaFX 边框标题

java - [JavaFx]这个绑定(bind)有什么问题?

java - 将 javax 验证与 JavaFX 集成

java - 无法使用 ch.qos.logback.classic 类

java - 使用 String 初始化 Float 或执行 parseFloat 有什么区别?

css - 如何在 JFoenix 时间选择器中编辑小时和分钟单元格

java - 如何正确关闭 javafx Alerts/fileChooser 等