Use of JAVA JTree __java

Source: Internet
Author: User
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import Javax.swing.JFrame;
Import Javax.swing.JMenuItem;
Import Javax.swing.JOptionPane;
Import Javax.swing.JPopupMenu;
Import Javax.swing.JTree;
Import Javax.swing.tree.DefaultMutableTreeNode;
Import Javax.swing.tree.DefaultTreeModel;




/**
* 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;
}

Defaultmutabletreenode node = (defaultmutabletreenode) getlastselectedpathcomponent ();

Defaultmutabletreenode subnode = new Defaultmutabletreenode (Strsubnode);
Node.add (subnode);
UpdateUI ();
}

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);
}

}
Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.