[Java] Swing tree-shaped component JTree

Source: Internet
Author: User

A simple style

Treenodedemo.java

 PackageCOM.SJF;ImportJavax.swing.JFrame;ImportJavax.swing.JTree;ImportJavax.swing.event.TreeSelectionEvent;ImportJavax.swing.event.TreeSelectionListener;ImportJavax.swing.tree.DefaultMutableTreeNode; Public  class treenodedemo {     Public Static void Main(string[] args) {//Create a tree node with no parent and child nodes, but allow child nodes, and initialize it with the specified user object.         //Public Defaultmutabletreenode (Object UserObject)Defaultmutabletreenode group1 =NewDefaultmutabletreenode ("Software Department"); TreeNode Node1 =NewTreeNode (); Node1.setname ("Wangyu Lotus"); Node1.setnickname ("Flying in the Sky"); Group1.add (NewDefaultmutabletreenode (Node1)); TreeNode Node2 =NewTreeNode (); Node2.setname ("Mengjia"); Node2.setnickname ("Dream"); Group1.add (NewDefaultmutabletreenode (Node2)); TreeNode Node3 =NewTreeNode (); Node3.setname ("Officer Fei er"); Node3.setnickname ("Fei er"); Group1.add (NewDefaultmutabletreenode (NODE3)); Defaultmutabletreenode group2 =NewDefaultmutabletreenode ("Sales Department"); TreeNode node4 =NewTreeNode (); Node4.setname ("The officer Wan Son"); Node4.setnickname ("Wan er"); Group2.add (NewDefaultmutabletreenode (NODE4)); TreeNode NODE5 =NewTreeNode (); Node5.setname ("Upper Nathar"); Node5.setnickname ("Qiao Son"); Group2.add (NewDefaultmutabletreenode (NODE5)); Defaultmutabletreenode top =NewDefaultmutabletreenode ("Staff Management");        Top.add (group1); Top.add (group2);FinalJTree tree =NewJTree (top); JFrame f =NewJFrame ("Jtreedemo");        F.add (tree); F.setsize ( -, -); F.setvisible (true); F.setdefaultcloseoperation (Jframe.exit_on_close);//Add Selection EventsTree.addtreeselectionlistener (NewTreeselectionlistener () {@Override             Public void valuechanged(Treeselectionevent e) {Defaultmutabletreenode node = (defaultmutabletreenode) tree.getlastselectedpathcomponent ();if(node = =NULL){return; }//ifObject object = Node.getuserobject ();                TreeNode user = (TreeNode) object; System.out.println ("You have chosen:"+ user.tostring ());    }        }); }}

Treenode.java

Package COM.SJF; Public classTreeNode {PrivateString nickname ="";PrivateString name ="";PrivateString phone ="";Private intAge PublicStringGetName() {returnName } Public void SetName(String name) { This. name = name; } PublicStringGetnickname() {returnNickname; } Public void Setnickname(String nickname) { This. nickname = nickname; } PublicStringGetphone() {returnPhone } Public void Setphone(String phone) { This. phone = phone; } Public int Getage() {returnAge } Public void Setage(intAge) { This. Age = Age; }//Focus on ToString, the display text of the node is ToString     PublicStringtoString()    {returnName +"["+nickname+"]"; }}

Two icon Styles

Treenode.java

Package COM.SJF; Public classTreeNode {PrivateString nickname ="";PrivateString name ="";PrivateString phone ="";Private intAgePrivateString ImagePath =""; PublicStringGetimagepath() {returnImagePath; } Public void setImagePath(String ImagePath) { This. ImagePath = ImagePath; } PublicStringGetName() {returnName } Public void SetName(String name) { This. name = name; } PublicStringGetnickname() {returnNickname; } Public void Setnickname(String nickname) { This. nickname = nickname; } PublicStringGetphone() {returnPhone } Public void Setphone(String phone) { This. phone = phone; } Public int Getage() {returnAge } Public void Setage(intAge) { This. Age = Age; }//Focus on ToString, the display text of the node is ToString     PublicStringtoString()    {returnName +"["+nickname+"]"; }}

Treecellrenderer.java

 PackageCOM.SJF;ImportJava.awt.Component;ImportJavax.swing.ImageIcon;ImportJavax.swing.JTree;ImportJavax.swing.tree.DefaultMutableTreeNode;ImportJavax.swing.tree.DefaultTreeCellRenderer;/** * Custom Tree Description class, set each node of the tree to a different icon * * / Public  class treecellrenderer extends defaulttreecellrenderer{    Private Static Final LongSerialversionuid =1L/** * Method of overriding parent class Defaulttreecellrenderer * /    @Override     PublicComponentgettreecellrenderercomponent(JTree tree, Object value,BooleanSelectedBooleanExpandedBooleanIsLeaf,intRowBooleanHasfocus) {//Selected        if(selected)        {Setforeground (Gettextselectioncolor ()); }Else{Setforeground (Gettextnonselectioncolor ()); }//TreeNodeDefaultmutabletreenode TreeNode = (defaultmutabletreenode) value; Object obj = Treenode.getuserobject ();if(objinstanceofTreeNode) {TreeNode node = (TreeNode) obj; Defaulttreecellrenderer Tempcellrenderer =NewDefaulttreecellrenderer (); Tempcellrenderer.setleaficon (NewImageIcon (Node.getimagepath ()));returnTempcellrenderer.gettreecellrenderercomponent (tree, Node.tostring (), selected, expanded,true, row, Hasfocus); }Else if(objinstanceofString) {String text = (string) obj; Defaulttreecellrenderer Tempcellrenderer =NewDefaulttreecellrenderer (); Tempcellrenderer.setopenicon (NewImageIcon ("Image/open.jpg")); Tempcellrenderer.setclosedicon (NewImageIcon ("Image/close.jpg"));returnTempcellrenderer.gettreecellrenderercomponent (tree, text, selected, expanded,false, row, Hasfocus); }return Super. gettreecellrenderercomponent (tree, value, selected, expanded, isleaf, row, hasfocus); }}

Treenodedemo.java

Package Com.sjf;import Javax.swing.jframe;import Javax.swing.jtree;import javax.swing.Event. Treeselectionevent;import javax.swing.Event. Treeselectionlistener;import Javax.swing.tree.DefaultMutableTreeNode; Public classTreenodedemo { Public Static void Main(string[] args) {//Create a tree node with no parent and child nodes, but allow child nodes, and initialize it with the specified user object.         //Public Defaultmutabletreenode (Object UserObject)Defaultmutabletreenode group1 =NewDefaultmutabletreenode ("Software Department"); TreeNode Node1 =NewTreeNode (); Node1.setname ("Wangyu Lotus"); Node1.setnickname ("Flying in the Sky"); Node1.setimagepath ("Image/1.jpg"); Group1.add (NewDefaultmutabletreenode (Node1)); TreeNode Node2 =NewTreeNode (); Node2.setname ("Mengjia"); Node2.setnickname ("Dream"); Node2.setimagepath ("Image/2.jpg"); Group1.add (NewDefaultmutabletreenode (Node2)); TreeNode Node3 =NewTreeNode (); Node3.setname ("Officer Fei er"); Node3.setnickname ("Fei er"); Node3.setimagepath ("Image/3.jpg"); Group1.add (NewDefaultmutabletreenode (NODE3)); Defaultmutabletreenode group2 =NewDefaultmutabletreenode ("Sales Department"); TreeNode node4 =NewTreeNode (); Node4.setname ("The officer Wan Son"); Node4.setnickname ("Wan er"); Node4.setimagepath ("Image/4.jpg"); Group2.add (NewDefaultmutabletreenode (NODE4)); TreeNode NODE5 =NewTreeNode (); Node5.setname ("Upper Nathar"); Node5.setnickname ("Qiao Son"); Node5.setimagepath ("Image/5.jpg"); Group2.add (NewDefaultmutabletreenode (NODE5)); Defaultmutabletreenode root =NewDefaultmutabletreenode ();        Root.add (group1); Root.add (group2);//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Final JTree tree =NewJTree (root); Tree.setcellrenderer (NewTreecellrenderer ()); Tree.putclientproperty ("Jtree.linestyle","None"); Tree.setrootvisible (false); JFrame frame =NewJFrame ("Jtreedemo");        Frame.add (tree); Frame.setsize ( -, -); Frame.setvisible (true); Frame.setdefaultcloseoperation (Jframe.exit_on_close);//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++        //Add Selection EventsTree.addtreeselectionlistener (NewTreeselectionlistener () {@Override Public void valuechanged(Treeselectionevent e) {Defaultmutabletreenode node = (defaultmutabletreenode) tree.getlastselectedpathcomponent ();if(node = =NULL){return; }//ifObjectObject= Node.getuserobject ();if(Objectinstanceof TreeNode) {TreeNode user = (TreeNode)Object; System. out. println ("You clicked on:"+ user.tostring ()); }Else if(ObjectInstanceof string) {string text = (string)Object; System. out. println ("You clicked on:"+ text);    }            }        }); }}

[Java] Swing tree-shaped component JTree

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.