To add an element dynamically , we will use three different classes.
Design Diagram for Tree:-
/**
* Creates new form TreeViewExample
*/
DefaultTreeModel model;
public TreeViewExample() {
initComponents();
model = (DefaultTreeModel) jTree1.getModel();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
DefaultMutableTreeNode root = (DefaultMutableTreeNode) jTree1.getModel().getRoot();
DefaultMutableTreeNode child = new DefaultMutableTreeNode(jTextField1.getText());
model.insertNodeInto(child, root, root.getChildCount());
}
}
POST Answer of Questions and ASK to Doubt