How To Add A New Node To JTree Using Java NetBeans
In this Java Tutorial we will see How To Insert A New Node To JTree From JTextField Using DefaultMutableTreeNode + TreeSelectionModel On Button Click Event In Java NetBeans .
Project Source Code:
private void jButtonAddNodeActionPerformed(java.awt.event.ActionEvent evt) {
// get selected Node
TreeSelectionModel smd = jTree1.getSelectionModel();
if(smd.getSelectionCount() > 0){
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) jTree1.getSelectionPath().getLastPathComponent();
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(jTextField1.getText());
selectedNode.add(newNode);
DefaultTreeModel model = (DefaultTreeModel) jTree1.getModel();
model.reload();
}
}
OutPut:
Download Projects Source Code