Click here to Skip to main content
13,047,567 members (62,401 online)
Rate this:
Please Sign up or sign in to 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 :

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
        //Register button object
        //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.setSize(500, 500);
    public void actionPerformed(ActionEvent e) {
        if (btnCreateNode.equals(e.getSource())) {
            if (chkNode.isSelected() == true) {
                ch1 = new JCheckBox(textBox.getText());
                TreeCellRenderer render = new TreeCellRenderer() {
                    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();
                        returnValue = ch1;
                        return returnValue;
                JOptionPane.showMessageDialog(this, ch1);
                JTree t = new JTree();
                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() {
            protected Object doInBackground() throws Exception {
                ((DefaultTreeModel) tree.getModel()).reload();
                return null;
Posted 6-Mar-13 2:15am

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 6 Mar 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100