PackageCom.swing.demo;Importjava.awt.BorderLayout;ImportJava.awt.Container;Importjava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;ImportJava.awt.event.MouseAdapter;Importjava.awt.event.MouseEvent;ImportJavax.swing.CellEditor;ImportJavax.swing.JButton;ImportJavax.swing.JFrame;ImportJavax.swing.JLabel;ImportJavax.swing.JPanel;ImportJavax.swing.JScrollPane;ImportJavax.swing.JTree;ImportJavax.swing.event.CellEditorListener;Importjavax.swing.event.ChangeEvent;Importjavax.swing.event.TreeModelEvent;ImportJavax.swing.event.TreeModelListener;ImportJavax.swing.tree.DefaultMutableTreeNode;ImportJavax.swing.tree.DefaultTreeModel;ImportJavax.swing.tree.TreeNode;ImportJavax.swing.tree.TreePath;/*** Tree node Add, delete, modify * *@authorAdministrator **/ Public classJtreetestImplementsActionListener, Treemodellistener {PrivateJLabel Statuslabel =NULL; PrivateJTree tree =NULL; PrivateDefaulttreemodel TreeModel =NULL; PrivateString Oldnodename =NULL; Publicjtreetest () {JFrame frame=NewJFrame ("jtreetest form"); Container ContentPane=Frame.getcontentpane (); Defaultmutabletreenode Root=NewDefaultmutabletreenode ("Root node"); Tree=NewJTree (root); Tree.addmouselistener (NewMytreemouselistener ()); TreeModel=(Defaulttreemodel) Tree.getmodel (); Treemodel.addtreemodellistener ( This); Tree.seteditable (true); Tree.getcelleditor (). Addcelleditorlistener (NewMytreecelleditorlistener ()); JScrollPane ScrollPane=NewJScrollPane (); Scrollpane.setviewportview (tree); JPanel Toolbarpanel=NewJPanel (); JButton b=NewJButton ("New node"); B.addactionlistener ( This); Toolbarpanel.add (b); b=NewJButton ("Delete node"); B.addactionlistener ( This); Toolbarpanel.add (b); b=NewJButton ("Clear All Nodes"); B.addactionlistener ( This); Toolbarpanel.add (b); Statuslabel=NewJLabel ("Action"); Contentpane.add (Toolbarpanel, Borderlayout.north); Contentpane.add (ScrollPane, Borderlayout.center); Contentpane.add (Statuslabel, Borderlayout.south); Frame.pack (); Frame.setvisible (true); Frame.requestfocus (); Frame.setsize (400, 300); Frame.setlocationrelativeto (NULL); Frame.setdefaultcloseoperation (Jframe.exit_on_close); } //This method runs the program code that adds, deletes, and clears all nodes. Public voidactionperformed (ActionEvent ae) {if(Ae.getactioncommand (). Equals ("New node") {Defaultmutabletreenode parentnode=NULL; Defaultmutabletreenode NewNode=NewDefaultmutabletreenode ("New Node")); Newnode.setallowschildren (true); TreePath Parentpath=Tree.getselectionpath (); if(Parentpath = =NULL) { return; } //Gets the parent node of the new nodeParentNode =( Defaultmutabletreenode) (Parentpath. Getlastpathcomponent ()); //adding new nodes by Defaulttreemodel's Insertnodeinto () methodTreemodel.insertnodeinto (NewNode, ParentNode, Parentnode.getchildcount ()); //The Tree's Scrollpathtovisible () method causes the tree to automatically expand the folder to display the new node that it joins. If this line is not added, the new node will be added//is wrapped in a folder and you have to expand the folder yourself to see it. Tree.scrollpathtovisible (NewTreePath (Newnode.getpath ())); Tree.setselectionpath (NewTreePath (Newnode.getpath ())); Statuslabel.settext ("New node succeeded"); } if(Ae.getactioncommand (). Equals ("Delete node") {TreePath TreePath=Tree.getselectionpath (); if(TreePath! =NULL) { //the following two lines get the parent node of the selected node.Defaultmutabletreenode Selectionnode =(Defaultmutabletreenode) TreePath. Getlastpathcomponent (); TreeNode Parent=(TreeNode) selectionnode.getparent (); if(Parent! =NULL) { //The removenodefromparent () method of Defaulttreemodel deletes the node, including its child nodes. treemodel.removenodefromparent (Selectionnode); Statuslabel.settext ("Delete node succeeded"); } } } if(Ae.getactioncommand (). Equals ("Clear all Nodes")) { //The following line obtains the root node from the Defaulttreemodel Getroot () method.Defaultmutabletreenode RootNode =(Defaultmutabletreenode) TreeModel. Getroot (); //The following line deletes all child nodes.Rootnode.removeallchildren (); //Be sure to run Defaulttreemodel's reload () operation after you delete it, and the entire tree node will be really deleted.treemodel.reload (); Statuslabel.settext ("Clear all nodes succeeded"); } } Public voidtreenodeschanged (treemodelevent e) {TreePath TreePath=E.gettreepath (); Defaultmutabletreenode node=(Defaultmutabletreenode) TreePath. Getlastpathcomponent (); Try { int[] index =e.getchildindices (); Node= (Defaultmutabletreenode) node.getchildat (index[0]); } Catch(NullPointerException exc) {} statuslabel.settext (Oldnodename+ "Change data to:" +(String) node.getuserobject ()); } Public Static voidMain (string[] args) {Newjtreetest (); } classMytreemouselistenerextendsMouseadapter { Public voidmousepressed (MouseEvent e) {Try{jtree Tree=(JTree) E.getsource (); intRowlocation =tree.getrowforlocation (E.getx (), e.gety ()); TreePath TreePath=Tree.getpathforrow (rowlocation); TreeNode TreeNode=(TreeNode) treepath.getlastpathcomponent (); Oldnodename=treenode.tostring (); } Catch(NullPointerException ne) {}}} classMytreecelleditorlistenerImplementsCelleditorlistener { Public voideditingstopped (ChangeEvent e) {Object Selectnode=tree.getlastselectedpathcomponent (); Defaultmutabletreenode node=(Defaultmutabletreenode) Selectnode; Celleditor Celleditor=(Celleditor) E.getsource (); String NewName=(String) celleditor.getcelleditorvalue (); Node.setuserobject (NewName); Defaulttreemodel Model=(Defaulttreemodel) Tree.getmodel (); model.nodestructurechanged (node); } Public voideditingcanceled (ChangeEvent e) {editingstopped (e); }} @Override Public voidtreenodesinserted (Treemodelevent e) {} @Override Public voidtreenodesremoved (Treemodelevent e) {} @Override Public voidtreestructurechanged (Treemodelevent e) {}}
JTree Add, Delete, modify