当前位置:主页 > 软件编程 > JAVA代码 >

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

时间:2020-10-15 23:15:50 | 栏目: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程序设计有所帮助。

您可能感兴趣的文章:

相关文章