power_settings_new

how do I remove root node from JTree?

by
asked
0 votes
252 views
I am currently removing all children from root using below line of code.

        //Remove all children from root
        DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
        DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
        root.removeAllChildren();
        model.reload();

Above code does not remove root from JTree. how do I remove root node from JTree?

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.

1 Answer

answered by (171 points)
selected by
0 votes
 
Best answer

Code written below should help you out.

 

DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
        DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
        root.removeAllChildren();
        model.reload();
        model.setRoot(null); // This will remove your root.

let me know

add
...