//先选中节点才能增加节点
import javaawt;
import javaawtevent;
import javaxswing;
import javaxswingevent;
import javaxswingtree;
public class TreeTest implements ActionListener,TreeModelListener{
JLabel label=null;
JTree tree=null;
DefaultTreeModel treeModel=null;
String nodeName=null;//原有节点名称
public TreeTest(){
JFrame f=new JFrame("TreeTest");
Container contentPane=fgetContentPane();
DefaultMutableTreeNode root=new DefaultMutableTreeNode("资源管理器");
tree=new JTree(root);
treesetEditable(true);
treeaddMouseListener(new MouseHandle());
treeModel=(DefaultTreeModel)treegetModel();
treeModeladdTreeModelListener(this);
JScrollPane scrollPane=new JScrollPane();
scrollPanesetViewportView(tree);
JPanel panel=new JPanel();
JButton b=new JButton("新增节点");
baddActionListener(this);
paneladd(b);
b=new JButton("删除节点");
baddActionListener(this);
paneladd(b);
b=new JButton("清除所有节点");
baddActionListener(this);
paneladd(b);
label=new JLabel("Action");
contentPaneadd(panel,BorderLayoutNORTH);
contentPaneadd(scrollPane,BorderLayoutCENTER);
contentPaneadd(label,BorderLayoutSOUTH);
fpack();
fsetVisible(true);
faddWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
Systemexit(0);
}
});
}
//本方法运行新增、删除、清除所有节点的程序代码
public void actionPerformed(ActionEvent ae){
if (aegetActionCommand()equals("新增节点")){
DefaultMutableTreeNode parentNode=null;
DefaultMutableTreeNode newNode=new DefaultMutableTreeNode("新节点");
newNodesetAllowsChildren(true);
TreePath parentPath=treegetSelectionPath();
//取得新节点的父节点
parentNode=(DefaultMutableTreeNode)(parentPathgetLastPathComponent());
//由DefaultTreeModel的insertNodeInto()方法增加新节点
treeModelinsertNodeInto(newNode,parentNode,parentNodegetChildCount());
//tree的scrollPathToVisible()方法在使Tree会自动展开文件夹以便显示所加入的新节点。若没加这行则加入的新节点
//会被 包在文件夹中,你必须自行展开文件夹才看得到。
treescrollPathToVisible(new TreePath(newNodegetPath()));
labelsetText("新增节点成功");
}
if (aegetActionCommand()equals("删除节点")){
TreePath treepath=treegetSelectionPath();
if (treepath!=null){
//下面两行取得选取节点的父节点
DefaultMutableTreeNode selectionNode=(DefaultMutableTreeNode)treepathgetLastPathComponent();
TreeNode parent=(TreeNode)selectionNodegetParent();
if (parent!=null) {
//由DefaultTreeModel的removeNodeFromParent()方法删除节点,包含它的子节点。
treeModelremoveNodeFromParent(selectionNode);
labelsetText("删除节点成功");
}
}
}
if (aegetActionCommand()equals("清除所有节点")){
//下面一行,由DefaultTreeModel的getRoot()方法取得根节点
DefaultMutableTreeNode rootNode=(DefaultMutableTreeNode)treeModelgetRoot();
//下面一行删除所有子节点
rootNoderemoveAllChildren();
//删除完后务必运行DefaultTreeModel的reload() *** 作,整个Tree的节点才会真正被删除
treeModelreload();
labelsetText("清除所有节点成功");
}
}
public void treeNodesChanged(TreeModelEvent e){
TreePath treePath=egetTreePath();
DefaultMutableTreeNode node=(DefaultMutableTreeNode)treePathgetLastPathComponent();
try{
int[] index=egetChildIndices();
node=(DefaultMutableTreeNode)nodegetChildAt(index[0]);
}catch(NullPointerException exc){}
labelsetText(nodeName+"更改数据为:"+(String)nodegetUserObject());
}
public void treeNodesInserted(TreeModelEvent e){
Systemoutprintln("new node insered");
}
public void treeNodesRemoved(TreeModelEvent e){
Systemoutprintln("node deleted");
}
public void treeStructureChanged(TreeModelEvent e){
Systemoutprintln("Structrue changed");
}
public static void main(String[] args){
new TreeTest();
}
class MouseHandle extends MouseAdapter{
public void mousePressed(MouseEvent e){
try{
JTree tree=(JTree)egetSource();
int rowLocation=treegetRowForLocation(egetX(),egetY());
TreePath treepath=treegetPathForRow(rowLocation);
TreeNode treenode=(TreeNode)treepathgetLastPathComponent();
nodeName=treenodetoString();
}catch(NullPointerException ne){}
}
}
}
以上就是关于java 动态的给树添加新节点 望高手指点啊全部的内容,包括:java 动态的给树添加新节点 望高手指点啊、、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)