How to add dynamic files to JTree

By | 2017-11-19T01:48:30+00:00 November 19th, 2017|java|0 Comments

In the previous tutorial, we learn about a simple JTree. This tutorial is for adding dynamic files to JTree.

Add dynamic files to JTree :

import javax.swing.*;
import javax.swing.tree.*;

import java.awt.*;
import java.awt.event.*;

class MyFrameTreeDemo extends JFrame implements ActionListener {
    DefaultMutableTreeNode root, parent, child, node;
    JTree tree;
    Container container;
    JButton add, remove;
    JPanel buttonsPanel;
    TreePath treePath;
    int index;

    MyFrameTreeDemo() {
        super("JTree Event Demo");

        root = new DefaultMutableTreeNode("India");

        parent = new DefaultMutableTreeNode("Andhra Pradesh");

        child = new DefaultMutableTreeNode("Hyderabad");

        child = new DefaultMutableTreeNode("Vizag");


        parent = new DefaultMutableTreeNode("Tamil Nadu");

        child = new DefaultMutableTreeNode("Chennai");


        tree = new JTree(root);

        add = new JButton("Add");
        remove = new JButton("Remove");

        buttonsPanel = new JPanel();

        container = getContentPane();
        container.add(new JScrollPane(tree));
        container.add(buttonsPanel, BorderLayout.SOUTH);

        setSize(400, 400);

    public void actionPerformed(ActionEvent ae) {
        if (ae.getSource() == add) {
            DefaultMutableTreeNode SelectedNode;

            treePath = tree.getSelectionPath();
            SelectedNode = (DefaultMutableTreeNode) treePath

            index = SelectedNode.getIndex(SelectedNode) + 1;

            String NodeStr = JOptionPane.showInputDialog(this,
                    "Enter the node value", "New Tree Node",

            node = new DefaultMutableTreeNode(NodeStr);
            SelectedNode.insert(node, index);
        } else if (ae.getSource() == remove) {
            int val = JOptionPane.showConfirmDialog(buttonsPanel,
                    "Please confirm to delete ?");
            if (val == 0) {
                DefaultMutableTreeNode SelectedNode;

                treePath = tree.getSelectionPath();
                SelectedNode = (DefaultMutableTreeNode) treePath
                if (SelectedNode.isLeaf()) {
                    parent = (DefaultMutableTreeNode) SelectedNode.getParent();
                } else {
                    JOptionPane.showMessageDialog(this, "Unable to Remove");


class TreeDemo {
    public static void main(String args[]) throws Exception {
        MyFrameTreeDemo frame = new MyFrameTreeDemo();


Output :

add dynamic files to JTree

Add File to JTree :

add dynamic files to JTree 1

After Adding File to JTree :

add dynamic files to JTree 2

Delete File from JTree :

add dynamic files to JTree 3

After Deleting File from JTree :

add dynamic files to JTree 4

Happy Learning 🙂

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment