Java examples for Swing:JTree
Removing a Node to a JTree Component
import javax.swing.JTree; import javax.swing.text.Position; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.MutableTreeNode; import javax.swing.tree.TreePath; public class Main { public void main(String[] argv) { JTree tree = new JTree(); DefaultTreeModel model = (DefaultTreeModel) tree.getModel(); // Find node to remove int startRow = 0; String prefix = "b"; TreePath path = tree.getNextMatch(prefix, startRow, Position.Bias.Forward); MutableTreeNode node = (MutableTreeNode) path.getLastPathComponent(); // Remove node and all descendants are removed as well model.removeNodeFromParent(node);//from w w w .jav a2s. com // The root cannot be removed with removeNodeFromParent(); // use the following to remove the root model.setRoot(null); } }