Java JTree remove tree node by user object
import java.awt.Container; import java.awt.FlowLayout; import java.util.ArrayList; import java.util.Iterator; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.TreeModel; public class Main { public static void main(String args[]) { final JFrame f = new JFrame("JTree Demo"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = f.getContentPane(); c.setLayout(new FlowLayout()); DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root"); DefaultMutableTreeNode c1 = new DefaultMutableTreeNode("X"); DefaultMutableTreeNode c2 = new DefaultMutableTreeNode("B"); DefaultMutableTreeNode c3 = new DefaultMutableTreeNode("C"); root.add(c1);/*from w w w . j av a 2s . c o m*/ root.add(c2); root.add(c3); c1.add(new DefaultMutableTreeNode("1")); c1.add(new DefaultMutableTreeNode("2")); c2.add(new DefaultMutableTreeNode("3")); c2.add(new DefaultMutableTreeNode("4")); c3.add(new DefaultMutableTreeNode("5")); DefaultMutableTreeNode n6 = new DefaultMutableTreeNode("6"); n6.setUserObject("demo2s.com"); c3.add(n6); JTree t = new JTree(root); removeNode("demo2s.com", t); c.add(new JScrollPane(t)); f.pack(); f.setVisible(true); } /** * Remove a TreeNode containing the specified obj from a JTree. It is assumed * that all TreeNodes in the specified tree are DefaultMutableTreeNodes and its * TreeModel should be a DefaultTreeModel. * * @param obj the contained * @param tree the JTree whose TreeNodes are DefaultMutableTreeNodes. */ public static void removeNode(Object obj, JTree tree) { DefaultMutableTreeNode treeNode = searchNode(obj, tree); DefaultTreeModel model = (DefaultTreeModel) tree.getModel(); model.removeNodeFromParent(treeNode); } /** * Search a DefaultMutableTreeNode whose userObject is the specified obj. * * @param obj the userObject. * @param tree the JTree whose TreeNodes are DefaultMutableTreeNodes. * @return a DefaultMuatbleTreeNode whose userObject is obj. */ public static DefaultMutableTreeNode searchNode(Object obj, JTree tree) { TreeModel model = tree.getModel(); DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot(); if (root.getUserObject() == obj) return root; return searchNode(obj, root); } /** * A recursive method to search a DefaultMutableTreeNode that displays an Object * obj. */ public static DefaultMutableTreeNode searchNode(Object obj, DefaultMutableTreeNode parentNode) { DefaultMutableTreeNode treeNode = null; int size = parentNode.getChildCount(); // A list of nodes with children java.util.List parentNodes = new ArrayList(); for (int i = 0; i < size; i++) { treeNode = (DefaultMutableTreeNode) parentNode.getChildAt(i); if (treeNode.getUserObject() == obj) return treeNode; if (treeNode.getChildCount() > 0) parentNodes.add(treeNode); } for (Iterator it = parentNodes.iterator(); it.hasNext();) { treeNode = searchNode(obj, (DefaultMutableTreeNode) it.next()); if (treeNode != null) return treeNode; } return null; } }