Swing中JTree的学习

JTree的代码记录

/**
  * 添加新结点
  * @param treeModel
  * @param newChild
  * @param parent
  */
 public void addNode(String value)
 {
  TreePath path = tree.getSelectionPath();                                                   //获得结点路径
  MyTreeNode parent =(MyTreeNode) path.getLastPathComponent();  //获得结点路径中的最后一个结点
  MyTreeNode child = new MyTreeNode(value);         
  treeModel.insertNodeInto(child, parent, parent.getChildCount());        //添加新结点
  tree.expandPath(path);                                                                                  //将结点展开
 }

 

/**
  * 移除结点
  * @param node
  */
 public void removeNode()
 {
  TreePath path = tree.getSelectionPath();                                                      //获得路径           
  treeModel.removeNodeFromParent((MyTreeNode)path.getLastPathComponent());  //根据路径从树上删除
 }

你可能感兴趣的:(swing,String,Path)