Remove node from TreeView


Question

I want to create this simple example of javaFX TreeView with context menu which can remove nodes from the tree:

public class TreeViewSample extends Application {

    private final Node rootIcon = new ImageView(
        new Image(getClass().getResourceAsStream("folder_16.png"))
    );

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

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Tree View Sample");        

        TreeItem<String> rootItem = new TreeItem<String> ("Inbox", rootIcon);
        rootItem.setExpanded(true);
        for (int i = 1; i < 6; i++) {
            TreeItem<String> item = new TreeItem<String> ("Message" + i);            
            rootItem.getChildren().add(item);
        }        
        TreeView<String> tree = new TreeView<String> (rootItem);        
        StackPane root = new StackPane();
        root.getChildren().add(tree);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }
}

I tested this context menu to remove right click selected node:

final ContextMenu contextMenu = new ContextMenu();
        MenuItem item1 = new MenuItem("About");
        item1.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent e)
            {
                System.out.println("About");
            }
        });
        MenuItem item2 = new MenuItem("Preferences");
        item2.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent e)
            {
                System.out.println("Preferences");
            }
        });
        MenuItem item3 = new MenuItem("Remove");
        item3.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent e)
            {

                DynamicTreeNodeModel c = treeView.getSelectionModel().getSelectedItem().getValue();
                boolean remove = treeView.getSelectionModel().getSelectedItem().getChildren().remove(c);
                System.out.println("Remove");
            }
        });
        contextMenu.getItems().addAll(item1, item2, item3);

        treeView.setContextMenu(contextMenu);

For some reason the code is not working. Can you help me to fix this issue?

1
2
3/1/2014 5:02:20 PM

Accepted Answer

You're trying to remove the selected node from it's own children. Since it doesn't exist there, nothing happens. You need to remove the selected node from it's parent's children.

    MenuItem item3 = new MenuItem("Remove");
    item3.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent e) {
            TreeItem c = (TreeItem)treeView.getSelectionModel().getSelectedItem();
            boolean remove = c.getParent().getChildren().remove(c);
            System.out.println("Remove");
        }
    });
8
3/1/2014 8:34:17 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon