Remove node from TreeView

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?

Answers


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

Need Your Help

Timing for numpad Android

java android

I am currently developing an application on android. Is there a way to detect if a number on the default numpad is pressed?

What is the best way to build a model/entity in Scala to map with Squeryl?

scala model playframework-2.0 entity squeryl

I'm very new to Scala, Play Framework and Squeryl. I already understand the concepts of val and var, but I'm having a hard time on trying to model my entities. As I saw on Squeryl documentation,

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.