/**
* A JTree maintenance code. Focus on
* 1) Get the way to select a node
* 2 the way to increase jpopmenu in JTree
* 3) Delete node
* 4) Modify the node
* @author Miao
*
*/
public class Showtree extends JTree {
Public Showtree (String rootname)
{
Super ();
Seteditable (TRUE);
Defaultmutabletreenode node = new Defaultmutabletreenode (rootname);
Defaulttreemodel TreeModel = new Defaulttreemodel (node);
Setmodel (TreeModel);
Addpopmenu ();
}
/**
* Add nodes to the data tree
* @param strsubnode
*/
public void Addtree (String strsubnode)
{
String inputstr = Joptionpane.showinputdialog ("Please enter a node name");
if (Inputstr.length () >0)
{
Strsubnode = Inputstr;
}
public void Addpopmenu ()
{
JPopupMenu PopupMenu = new JPopupMenu ();
JMenuItem item = new JMenuItem ("Add");
Item.addactionlistener (new ActionListener () {
@Override
public void actionperformed (ActionEvent e) {
TODO auto-generated Method Stub
Addtree ("Newsubnode");
}
});
Popupmenu.add (item);
item = new JMenuItem ("delete");
Item.addactionlistener (new ActionListener () {
@Override
public void actionperformed (ActionEvent arg0) {
TODO auto-generated Method Stub
deltree ();
}
});
Popupmenu.add (item);
item = new JMenuItem ("Modify");
Item.addactionlistener (new ActionListener () {
@Override
public void actionperformed (ActionEvent e) {
TODO auto-generated Method Stub
Edittree ();
}
});
Popupmenu.add (item);
Setcomponentpopupmenu (PopupMenu);
}
/**
* Only input carriage return to modify the node value of the tree structure
*/
private void Edittree ()
{
Starteditingatpath (Getselectionpath ());
}
public void deltree ()
{
int option = Joptionpane.showconfirmdialog (null, OK) Are you sure you want to delete this node? ");
if (option==0)
{
SYSTEM.OUT.PRINTLN (option);
Defaultmutabletreenode node = (defaultmutabletreenode) getlastselectedpathcomponent ();
Node.removefromparent ();
UpdateUI ();
}
}
public static void Main (string[] args)
{
Showtree showtree = new Showtree ("This is Root");
JFrame JFrame = new JFrame ();
Jframe.getcontentpane (). Add (Showtree);
Swingconsole.run (jframe,0, 0);
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.