在javafx中添加多个树分支和叶子

时间:2017-06-07 11:08:09

标签: javafx treeview

您好我之前发布此问题但没有得到任何答案。 Adding tree Branches and leafs in TreeView

我需要帮助,请查看代码,

public class Main extends Application
{
    private BorderPane border;
    @Override
    public void start(Stage primaryStage)
    {   
        border = new BorderPane();    
        Scene scene = new Scene(border,200,200); 
        primaryStage.setTitle("BorderPane");
        primaryStage.setScene(scene);
        primaryStage.show();    

        TreeItem<String> tree = new TreeItem<String>("Root");      
        TreeItem<String> item1 = new TreeItem<String>("Branch");
        item1.getChildren().add(new TreeItem<String>("Leaf"));
        item1.setExpanded(true); 
        tree.setExpanded(true);
        tree.getChildren().addAll(item1);
        TreeView<String> treeView  = new TreeView<String>(tree);

        treeView.setEditable(true);
        treeView.setCellFactory(new Callback<TreeView<String>,TreeCell<String>>(){
            @Override
            public TreeCell<String> call(TreeView<String> arg0) {
                return new AddMenuTreeCell();
            }
        });

        VBox vbox =new VBox(2);
        vbox.setPadding(new Insets(5));
        VBox.setVgrow(treeView, Priority.ALWAYS);
        vbox.getChildren().addAll(treeView);      
        border.setLeft(vbox);
    }

    private static class AddMenuTreeCell extends TextFieldTreeCell<String> {
        private ContextMenu menu = new ContextMenu();
        private TextField textField;
        public AddMenuTreeCell() {

            MenuItem addItem1 = new MenuItem("Insert Branch");
            MenuItem addItem2 = new MenuItem("Insert Leaf");
            menu.getItems().addAll(addItem1,addItem2);
            addItem1.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent arg0) {
                    TreeItem<String> newBranch = 
                            new TreeItem<String>("Brunch");
                                getTreeItem().getChildren().add(newBranch);
                }
            });
            addItem2.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent arg0) {
                    TreeItem<String> newLeaf = 
                            new TreeItem<String>("leaf");
                                getTreeItem().getChildren().add(newLeaf);
                }
            });
        }

        @Override
        public void updateItem(String item, boolean empty) {
            super.updateItem(item, empty);
                    if (!empty && getTreeItem().getParent() == null){
                        setContextMenu(menu);
                    }
                }
        }
} 

使用此用户可以添加多个分支和叶子。

但问题是,如果用户添加一个分支,它应该像“Branch1”,下一个添加的分支应该是“Branch2”,“Branch3”,......同样为任何分支中添加的叶子都有它们的数字。

以后用户可以将不同的任务分配给不同的分支和叶子。

谢谢!

1 个答案:

答案 0 :(得分:2)

将AddMenuTreeCell类替换为以下代码并立即尝试

private static class AddMenuTreeCell extends TextFieldTreeCell<String> {
    private ContextMenu menu = new ContextMenu();
    private TextField textField;
    int i = 1, j = 1;

    public AddMenuTreeCell() {
        MenuItem addItem1 = new MenuItem("Insert Branch");
        MenuItem addItem2 = new MenuItem("Insert Leaf");
        menu.getItems().addAll(addItem1, addItem2);

        addItem1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent arg0) {

                TreeItem<String> newBranch = new TreeItem<String>("Brunch" + i);
                getTreeItem().getChildren().add(newBranch);
                i++;
            }
        });
        addItem2.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent arg0) {
                TreeItem<String> newLeaf = new TreeItem<String>("leaf" + j);
                getTreeItem().getChildren().add(newLeaf);
                j++;
            }
        });
        setContextMenu(menu);
    }
}