Java Delete JTree Node

How To Remove Selected Node From A JTree Using Java NetBeans

remove jtree selected node using java



In this Java Tutorial we will see How To Delete The Selected JTree Node Using DefaultMutableTreeNode + DefaultTreeModel On Button Click Event In Java NetBeans .




Project Source Code:

private void jButtonRemoveNodeActionPerformed(java.awt.event.ActionEvent evt) {                                                  
        // get selected node
        DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) jTree1.getSelectionPath().getLastPathComponent();
        
        DefaultTreeModel model = (DefaultTreeModel) jTree1.getModel();
        
        if(selectedNode != jTree1.getModel().getRoot())
            model.removeNodeFromParent(selectedNode);
        else
            model.setRoot(null);
    } 


OutPut:

delete jtree selected node in java




Share this

Related Posts

Previous
Next Post »