Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java Swing
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
        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 6-Mar-13 3:15am
Edited 6-Mar-13 5:00am
(no name)113.6K
v2

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

  Print Answers RSS
0 BillWoodruff 360
1 Mathew Soji 309
2 DamithSL 225
3 Afzaal Ahmad Zeeshan 202
4 Maciej Los 190
0 OriginalGriff 6,249
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 3,865


Advertise | Privacy | Mobile
Web03 | 2.8.1411019.1 | Last Updated 6 Mar 2013
Copyright © CodeProject, 1999-2014
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