Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
1) how to add simple node (without check box control) in Jtree.

2) Or how to add Check Box node(with check box control) in Jtree.

3) all node is add in seam Tree using java swing.

4) Please suggest me for this task .

following is code sample :

Java
public class DynamicTreeView extends JFrame implements ActionListener, MouseListener {

    // cerate varible  
    //private CheckTreeSelectionModel selectionModel;
    private JTree tree;
    private JTextField textBox;
    private JButton btnCreateNode;
    private JCheckBox chkNode, ch1;
    private JScrollPane ScrollPan;
    private JPanel pane, pane1;
    private DefaultTreeModel model;
    private JCheckBox check;
    int hotspot;

    public DynamicTreeView() {
        hotspot = new JCheckBox().getPreferredSize().width;
        //cerate Panel object  
        pane = new JPanel();
        //cerate JTextField object  
        textBox = new JTextField(10);
        //cerate JButton object  
        btnCreateNode = new JButton("Create Node");
        //cerate JCheckBox object  
        chkNode = new JCheckBox("Has CheckBox");
        //add componet all to  panel
        pane.add(chkNode);
        pane.add(textBox);
        pane.add(btnCreateNode);
        //Register button object
        btnCreateNode.addActionListener(this);
        this.addMouseListener(this);
        //Cerate the Default Root Tree node for Tree
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("Java Tree Example");
        //Cerate the tree object with Default Root Tree node 

        tree = new JTree(root);
        //get the module from tree and add to the DefaultTreeModule
        model = (DefaultTreeModel) tree.getModel();
        //cerate scrollpane object with tree
        ScrollPan = new JScrollPane(tree);
        //scrollpane object add to the form
        //use the border lay out for this form scroll pane is add to the left side of the form
        getContentPane().add(ScrollPan, BorderLayout.WEST);
        //panel object add center of the form
        getContentPane().add(pane, BorderLayout.CENTER);
    }

    //Main method of the class it's the staring point of the class.
    public static void main(String args[]) {
        //set default look of the form on start position.
        DynamicTreeView frame = new DynamicTreeView();
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setSize(500, 500);
        frame.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (btnCreateNode.equals(e.getSource())) {

            if (chkNode.isSelected() == true) {
                ch1 = new JCheckBox(textBox.getText());
                TreeCellRenderer render = new TreeCellRenderer() {
                    @Override
                    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
                        Component returnValue;
                        if ((value != null) && (value instanceof DefaultMutableTreeNode)) {
                            Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
                            ch1.setText(textBox.getText());
                            ch1.setSelected(selected);
                        }
                        returnValue = ch1;
                        return returnValue;
                    }
                };
                JOptionPane.showMessageDialog(this, ch1);
                JTree t = new JTree();
                t.setCellRenderer(render);
                MutableTreeNode newNode = new DefaultMutableTreeNode(t);
                //model = (DefaultTreeModel) tree.getModel();
                TreePath path = tree.getSelectionPath();
                //MutableTreeNode newNode = new DefaultMutableTreeNode();
                MutableTreeNode node = (MutableTreeNode) path.getLastPathComponent();
                //model.insertNodeInto(cr, node, node.getChildCount());
            } else {
                CheckRenderer cr = new CheckRenderer();
                cr.setNode(new DefaultMutableTreeNode(textBox.getText()));
                model = (DefaultTreeModel) tree.getModel();
                TreePath path = tree.getSelectionPath();
                MutableTreeNode newNode = cr.getNode();
                MutableTreeNode node = (MutableTreeNode) path.getLastPathComponent();
                model.insertNodeInto(newNode, node, node.getChildCount());
            }
        }

        SwingWorker sw = new SwingWorker() {
            @Override
            protected Object doInBackground() throws Exception {
                ((DefaultTreeModel) tree.getModel()).reload();
                tree.revalidate();
                tree.repaint();
                ScrollPan.setViewportView(tree);
                return null;
            }
        };
        sw.execute();
    }
}
Posted
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900