Treelist摆脱了根源。应该只显示叶子

时间:2018-09-26 06:02:34

标签: java javafx root treelist

我正试图摆脱根源(Bucky和Megan)。如果我单击该按钮,它将不起作用。有人可以帮我吗?

叶子(迈克尔,YouTube,视频,唇膏,闪光)不应消失。

我不知道为什么它不起作用。

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Main extends Application
{
    Stage window;
    TreeView<String> tree;


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

    public void start(Stage primaryStage) throws Exception
    {
        window = primaryStage;
        window.setTitle("javaFX TreeView");


        TreeItem<String> root, bucky, megan;

        //Root
        root = new TreeItem<String>();
        root.setExpanded(true);


        //Bucky
        bucky = makeBranch ("Bucky", root);
        makeBranch ("Michael", bucky);
        makeBranch ("YouTube", bucky);
        makeBranch ("Video", bucky);


        //Megan
        megan = makeBranch ("Megan", root);
        makeBranch ("Lipstick", megan);
        makeBranch ("Glitter", megan);

        //Create tree
        tree = new TreeView<String>(root);
        tree.setShowRoot(false);

        //Layout
        final StackPane layout = new StackPane();
        Button button = new Button();
        button.setText("Knopf");
        layout.getChildren().add(tree);
        layout.getChildren().add(button);

        Scene scene = new Scene(layout, 300, 250);
        window.setScene(scene);
        window.show();

        button.setOnAction(new EventHandler<ActionEvent>()
        {
            public void handle(ActionEvent event)
            {
                tree.setShowRoot(false);
            }
        });
    }
    //Create branches
    public TreeItem<String> makeBranch(String title, TreeItem<String> parent){
        TreeItem<String> item = new TreeItem<String>(title);
        item.setExpanded(true);
        parent.getChildren().add(item);
        return item;
    }
}

0 个答案:

没有答案