http://developer.51cto.com/art/201201/313014.htm
http://blog.sina.com.cn/s/blog_40b22f640100kf3a.html
http://blog.csdn.net/cnjzy0106/article/details/5388715
//删除:
private void jButton8MouseClicked(java.awt.event.MouseEvent evt) {
DefaultTreeModel model = (DefaultTreeModel) jTree1.getModel();
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) jTree1
.getLastSelectedPathComponent();
if (selectedNode != null && selectedNode.getParent() != null) {
// 删除指定节点
model.removeNodeFromParent(selectedNode);
}
}
// 添加:
private void jButton7MouseClicked(java.awt.event.MouseEvent evt) {
DefaultTreeModel model = (DefaultTreeModel) jTree1.getModel();
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) jTree1
.getLastSelectedPathComponent();//获取选中节点
if (selectedNode == null)
return;
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("new node");//创建一个新节点
model.insertNodeInto(newNode, selectedNode,
selectedNode.getChildCount());
// DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selectedNode
// .getParent();
// if (parent == null)
// return;
//int selectedIndex = parent.getIndex(selectedNode);
//model.insertNodeInto(newNode, parent, selectedIndex + 1);
}
//编辑
jTree1.setEditable(true);
TreePath selectedPath = jTree1.getSelectionPath();
if (selectedPath != null) {
jTree1.startEditingAtPath(selectedPath); // 编辑选中节点
}
jTree1.setInvokesStopCellEditing(true);//设置可编辑
}