Java Transfer JTree Nodes To JTable

How To Set JTree Nodes Values Into JTable Rows Using Java NetBeans

Transfer JTree Nodes To JTable Using Java



In this Java Tutorial we will see How To Get JTree Nodes Data And Set It Into JTable Rows Using For Loop + DefaultMutableTreeNode + DefaultTreeModel On Button Click Event In Java NetBeans .




Project Source Code:

private void jButtonToJTableActionPerformed(java.awt.event.ActionEvent evt) {                                                
        // get jtree model
        DefaultTreeModel treeModel = (DefaultTreeModel) jTree1.getModel();
        // get jtable model
        DefaultTableModel tableModel = (DefaultTableModel) jTable1.getModel();
        // get jtree root
        DefaultMutableTreeNode root = (DefaultMutableTreeNode) treeModel.getRoot();

        String colName;
        
        for(int i = 0; i < treeModel.getChildCount(root); i++)
        {
            DefaultMutableTreeNode node1 = (DefaultMutableTreeNode) root.getChildAt(i);
            Object row[] = new Object[node1.getChildCount()];
            
            colName = node1.getUserObject().toString();
            
            for(int j = 0; j < node1.getChildCount(); j++){
               DefaultMutableTreeNode node2 = (DefaultMutableTreeNode) node1.getChildAt(j);
               row[j] = node2.getUserObject();
            }
            tableModel.addColumn(colName,row);
        }
        
    } 




Share this

Related Posts

Previous
Next Post »

1 comments:

comments