Java Swing树状组件JTree用法实例详解
程序员文章站
2023-12-17 21:42:10
本文实例讲述了java swing树状组件jtree用法。分享给大家供大家参考,具体如下:
一、简单样式
先看运行效果:
treenodedemo.java...
本文实例讲述了java swing树状组件jtree用法。分享给大家供大家参考,具体如下:
一、简单样式
先看运行效果:
treenodedemo.java
package awtdemo; import javax.swing.jframe; import javax.swing.jtree; import javax.swing.event.treeselectionevent; import javax.swing.event.treeselectionlistener; import javax.swing.tree.defaultmutabletreenode; public class treenodedemo { public static void main(string[] args) { // 创建没有父节点和子节点、但允许有子节点的树节点,并使用指定的用户对象对它进行初始化。 // public defaultmutabletreenode(object userobject) defaultmutabletreenode group1 = new defaultmutabletreenode("软件部"); treenode node1 = new treenode(); node1.setname("王雨"); node1.setnickname("漫天飞舞"); group1.add(new defaultmutabletreenode(node1)); treenode node2 = new treenode(); node2.setname("陈梦"); node2.setnickname("梦"); group1.add(new defaultmutabletreenode(node2)); treenode node3 = new treenode(); node3.setname("上官飞儿"); node3.setnickname("飞儿"); group1.add(new defaultmutabletreenode(node3)); defaultmutabletreenode group2 = new defaultmutabletreenode("销售部"); treenode node4 = new treenode(); node4.setname("上官婉儿"); node4.setnickname("婉儿"); group2.add(new defaultmutabletreenode(node4)); treenode node5 = new treenode(); node5.setname("上官巧儿"); node5.setnickname("巧儿"); group2.add(new defaultmutabletreenode(node5)); defaultmutabletreenode top = new defaultmutabletreenode("职员管理"); top.add(group1); top.add(group2); final jtree tree = new jtree(top); jframe f = new jframe("jtreedemo - www.jb51.net"); f.add(tree); f.setsize(300, 300); f.setvisible(true); f.setdefaultcloseoperation(jframe.exit_on_close); // 添加选择事件 tree.addtreeselectionlistener(new treeselectionlistener() { @override public void valuechanged(treeselectionevent e) { defaultmutabletreenode node = (defaultmutabletreenode) tree.getlastselectedpathcomponent(); if (node == null){ return; }//if object object = node.getuserobject(); treenode user = (treenode) object; system.out.println("你选择了:" + user.tostring()); } }); } }
treenode.java
package awtdemo; public class treenode { private string nickname = ""; private string name = ""; private string phone = ""; private int age; public string getname() { return name; } public void setname(string name) { this.name = name; } public string getnickname() { return nickname; } public void setnickname(string nickname) { this.nickname = nickname; } public string getphone() { return phone; } public void setphone(string phone) { this.phone = phone; } public int getage() { return age; } public void setage(int age) { this.age = age; } // 重点在tostring,节点的显示文本就是tostring public string tostring() { return name + "["+nickname+"]"; } }
二、图标样式
运行效果:
treenode.java
package awtdemo; public class treenode { private string nickname = ""; private string name = ""; private string phone = ""; private int age; private string imagepath = ""; public string getimagepath() { return imagepath; } public void setimagepath(string imagepath) { this.imagepath = imagepath; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getnickname() { return nickname; } public void setnickname(string nickname) { this.nickname = nickname; } public string getphone() { return phone; } public void setphone(string phone) { this.phone = phone; } public int getage() { return age; } public void setage(int age) { this.age = age; } // 重点在tostring,节点的显示文本就是tostring public string tostring() { return name + "["+nickname+"]"; } }
treecellrenderer.java
package awtdemo; import java.awt.component; import javax.swing.imageicon; import javax.swing.jtree; import javax.swing.tree.defaultmutabletreenode; import javax.swing.tree.defaulttreecellrenderer; /** * 自定义树描述类,将树的每个节点设置成不同的图标 * */ public class treecellrenderer extends defaulttreecellrenderer{ private static final long serialversionuid = 1l; /** * 重写父类defaulttreecellrenderer的方法 */ @override public component gettreecellrenderercomponent(jtree tree, object value, boolean selected, boolean expanded, boolean isleaf, int row,boolean hasfocus) { // 选中 if (selected) { setforeground(gettextselectioncolor()); } else { setforeground(gettextnonselectioncolor()); } // treenode defaultmutabletreenode treenode = (defaultmutabletreenode) value; object obj = treenode.getuserobject(); if (obj instanceof treenode) { treenode node = (treenode) obj; defaulttreecellrenderer tempcellrenderer = new defaulttreecellrenderer(); tempcellrenderer.setleaficon(new imageicon(node.getimagepath())); return tempcellrenderer.gettreecellrenderercomponent(tree, node.tostring(), selected, expanded, true, row, hasfocus); } else if (obj instanceof string) { string text = (string) obj; defaulttreecellrenderer tempcellrenderer = new defaulttreecellrenderer(); tempcellrenderer.setopenicon(new imageicon("image/open.jpg")); tempcellrenderer.setclosedicon(new imageicon("image/close.jpg")); return tempcellrenderer.gettreecellrenderercomponent(tree, text, selected, expanded, false, row, hasfocus); } return super.gettreecellrenderercomponent(tree, value, selected, expanded, isleaf, row, hasfocus); } }
treenodedemo.java
package awtdemo; import javax.swing.jframe; import javax.swing.jtree; import javax.swing.event.treeselectionevent; import javax.swing.event.treeselectionlistener; import javax.swing.tree.defaultmutabletreenode; public class treenodedemo { public static void main(string[] args) { // 创建没有父节点和子节点、但允许有子节点的树节点,并使用指定的用户对象对它进行初始化。 // public defaultmutabletreenode(object userobject) defaultmutabletreenode group1 = new defaultmutabletreenode("软件部"); treenode node1 = new treenode(); node1.setname("王雨"); node1.setnickname("漫天飞舞"); node1.setimagepath("image/1.jpg"); group1.add(new defaultmutabletreenode(node1)); treenode node2 = new treenode(); node2.setname("陈梦"); node2.setnickname("梦"); node2.setimagepath("image/2.jpg"); group1.add(new defaultmutabletreenode(node2)); treenode node3 = new treenode(); node3.setname("上官飞儿"); node3.setnickname("飞儿"); node3.setimagepath("image/3.jpg"); group1.add(new defaultmutabletreenode(node3)); defaultmutabletreenode group2 = new defaultmutabletreenode("销售部"); treenode node4 = new treenode(); node4.setname("上官婉儿"); node4.setnickname("婉儿"); node4.setimagepath("image/4.jpg"); group2.add(new defaultmutabletreenode(node4)); treenode node5 = new treenode(); node5.setname("上官巧儿"); node5.setnickname("巧儿"); node5.setimagepath("image/5.jpg"); group2.add(new defaultmutabletreenode(node5)); defaultmutabletreenode root = new defaultmutabletreenode(); root.add(group1); root.add(group2); //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ final jtree tree = new jtree(root); tree.setcellrenderer(new treecellrenderer()); tree.putclientproperty("jtree.linestyle", "none"); tree.setrootvisible(true); jframe frame = new jframe("jtreedemo - www.jb51.net"); frame.add(tree); frame.setsize(300, 300); frame.setvisible(true); frame.setdefaultcloseoperation(jframe.exit_on_close); //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // 添加选择事件 tree.addtreeselectionlistener(new treeselectionlistener() { @override public void valuechanged(treeselectionevent e) { defaultmutabletreenode node = (defaultmutabletreenode) tree.getlastselectedpathcomponent(); if (node == null){ return; }//if object object = node.getuserobject(); if(object instanceof treenode) { treenode user = (treenode) object; system.out.println("你点击了:" + user.tostring()); } else if(object instanceof string) { string text = (string)object; system.out.println("你点击了:" + text); } } }); } }
更多关于java相关内容感兴趣的读者可查看本站专题:《java数据结构与算法教程》、《java字符与字符串操作技巧总结》、《java操作dom节点技巧总结》、《java文件与目录操作技巧汇总》和《java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。