Java JTree remove tree node
import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTree; import javax.swing.event.TreeModelEvent; import javax.swing.event.TreeModelListener; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.MutableTreeNode; import javax.swing.tree.TreePath; class DynamicTree extends JPanel { DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Root Node"); DefaultTreeModel treeModel = new DefaultTreeModel(rootNode); JTree tree = new JTree(treeModel); public DynamicTree() { super(new GridLayout(1, 0)); //from w ww.j a va 2s . c o m treeModel.addTreeModelListener(new MyTreeModelListener()); tree.setEditable(true); tree.setShowsRootHandles(true); JScrollPane scrollPane = new JScrollPane(tree); add(scrollPane); } public void clear() { rootNode.removeAllChildren(); treeModel.reload(); } public void removeCurrentNode() { TreePath currentSelection = tree.getSelectionPath(); if (currentSelection != null) { DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode) (currentSelection.getLastPathComponent()); MutableTreeNode parent = (MutableTreeNode) (currentNode.getParent()); if (parent != null) { treeModel.removeNodeFromParent(currentNode); return; } } } public DefaultMutableTreeNode addObject(Object child) { DefaultMutableTreeNode parentNode = null; TreePath parentPath = tree.getSelectionPath(); if (parentPath == null) { parentNode = rootNode; } else { parentNode = (DefaultMutableTreeNode) (parentPath.getLastPathComponent()); } return addObject(parentNode, child, true); } public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent, Object child) { return addObject(parent, child, false); } public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent, Object child, boolean shouldBeVisible) { DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child); if (parent == null) { parent = rootNode; } treeModel.insertNodeInto(childNode, parent, parent.getChildCount()); if (shouldBeVisible) { tree.scrollPathToVisible(new TreePath(childNode.getPath())); } return childNode; } } class MyTreeModelListener implements TreeModelListener { public void treeNodesChanged(TreeModelEvent e) { DefaultMutableTreeNode node; node = (DefaultMutableTreeNode) (e.getTreePath().getLastPathComponent()); int index = e.getChildIndices()[0]; node = (DefaultMutableTreeNode) (node.getChildAt(index)); System.out.println("The user has finished editing the node."); System.out.println("New value: " + node.getUserObject()); } public void treeNodesInserted(TreeModelEvent e) { System.out.println(e); } public void treeNodesRemoved(TreeModelEvent e) { System.out.println(e); } public void treeStructureChanged(TreeModelEvent e) { System.out.println(e); } } public class Main extends JPanel implements ActionListener { private int newNodeSuffix = 1; private static String ADD_COMMAND = "add"; private static String REMOVE_COMMAND = "remove"; private static String CLEAR_COMMAND = "clear"; private DynamicTree treePanel; public Main() { super(new BorderLayout()); treePanel = new DynamicTree(); populateTree(treePanel); JButton addButton = new JButton("Add"); addButton.setActionCommand(ADD_COMMAND); addButton.addActionListener(this); JButton removeButton = new JButton("Remove"); removeButton.setActionCommand(REMOVE_COMMAND); removeButton.addActionListener(this); JButton clearButton = new JButton("Clear"); clearButton.setActionCommand(CLEAR_COMMAND); clearButton.addActionListener(this); // Lay everything out. treePanel.setPreferredSize(new Dimension(300, 150)); add(treePanel, BorderLayout.CENTER); JPanel panel = new JPanel(new GridLayout(0, 3)); panel.add(addButton); panel.add(removeButton); panel.add(clearButton); add(panel, BorderLayout.SOUTH); } public void populateTree(DynamicTree treePanel) { DefaultMutableTreeNode p1 = treePanel.addObject(null, "1"); DefaultMutableTreeNode p2 = treePanel.addObject(null, "2"); treePanel.addObject(p1, "C1"); treePanel.addObject(p1, "C2"); treePanel.addObject(p2, "C3"); treePanel.addObject(p2, "C4"); } public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); if (ADD_COMMAND.equals(command)) { treePanel.addObject("New Node " + newNodeSuffix++); } else if (REMOVE_COMMAND.equals(command)) { treePanel.removeCurrentNode(); } else if (CLEAR_COMMAND.equals(command)) { treePanel.clear(); } } public static void main(String[] args) { JFrame frame = new JFrame("DynamicTreeDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Main newContentPane = new Main(); frame.setContentPane(newContentPane); frame.pack(); frame.setVisible(true); } }