服务器之家

服务器之家 > 正文

Java Swing树状组件JTree用法实例详解

时间:2021-02-07 16:16     来源/作者:SunnyYoona

本文实例讲述了java swing树状组件jtree用法。分享给大家供大家参考,具体如下:

一、简单样式

先看运行效果:

Java Swing树状组件JTree用法实例详解

treenodedemo.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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.zzvips.com");
    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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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+"]";
  }
}

二、图标样式

运行效果:

Java Swing树状组件JTree用法实例详解

treenode.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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.zzvips.com");
    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程序设计有所帮助。

原文链接:http://blog.csdn.net/sunnyyoona/article/details/46292355

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部