Lompat ke konten Lompat ke sidebar Lompat ke footer

Java Add Node To JTree

How To Add A New Node To JTree Using Java NetBeans

 How To Add A New Node To JTree Using Java NetBeans Java Add Node To JTree



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:

 How To Add A New Node To JTree Using Java NetBeans Java Add Node To JTree




Posting Komentar untuk "Java Add Node To JTree"