创建树以及树的双击事件

package com.boomlink.design;

/**
* 树节点属性userObject的对象类
* @author 浪人
*
*/
public class TreeNodeObject{
   /**
    * 节点ID
    */
  String nodeId;    
   /**
    * 父辈ID
    */
  String parentId;    
   /**
    * 节点名称
    */
  String nodeName;    
   /**
    * 包含其他信息的对象
    */
  Object userObject;    
    
   /**
    * 返回树节点对象的显示名称
    */
   public String toString(){

     return nodeName;
  }
   /**
    * 返回树节点对象的主要信息
    * @return String
    */
   public String toString2(){
    String str = "[";
    str += "{节点ID:" + nodeId + "}";
    str += ",{父辈ID:" + parentId + "}";
    str += ",{节点名称:" + nodeName + "}";
    str += "]\n";
     return str;
  }
   public String getNodeId() {
     return nodeId;
  }
   public void setNodeId(String nodeId) {
     this.nodeId = nodeId;
  }
   public String getNodeName() {
     return nodeName;
  }
   public void setNodeName(String nodeName) {
     this.nodeName = nodeName;
  }
   public String getParentId() {
     return parentId;
  }
   public void setParentId(String parentId) {
     this.parentId = parentId;
  }
   public Object getUserObject() {
     return userObject;
  }
   public void setUserObject(Object userObject) {
     this.userObject = userObject;
  }
}
 
package com.boomlink.design;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

/**
* 用一个对象列表创建一颗树。
* @author 浪人
*
*/
public class MyTree {
   /**
    * 数据库驱动程序类名
    */
   static String classname = "oracle.jdbc.driver.OracleDriver";
   /**
    * 访问数据库URL
    */
   static String url = "jdbc:oracle:thin:@192.168.1.158:1521:saledb";
   /**
    * 数据库用户名
    */
   static String username = "SaleUser";
   /**
    * 数据库密码
    */
   static String password = "SaleUser";
   /**
    * 数据库连接
    */
   static Connection connection;
   /**
    * 加载数据库驱动程序类,创建一个数据库连接。
    */
   static {
     try{
      Class.forName(classname);
    } catch(ClassNotFoundException ex){
      ex.printStackTrace();
    }
     try {
      connection = DriverManager.getConnection(url, username, password);
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }
   /**
    * 对象列表
    */
  List<TreeNodeObject> treeDataList;
   /**
    * JTree
    */
  JTree jtree;
   /**
    * 构造方法
    */
   public MyTree(){
    treeDataList = getTreeData();
    DefaultMutableTreeNode root = new DefaultMutableTreeNode();
    jtree = new JTree(root);
    addChildren(root, "root");
    addClickEvent(jtree);
  }
   /**
    * 给树节点添加孩子
    * @param node DefaultMutableTreeNode
    * @param nodeId String
    */
   public void addChildren(DefaultMutableTreeNode node, String nodeId){
     /* 递归为树节点添加孩子。将treeDataList中元素的父辈ID值等于参数nodeId值的元素对象创建一个树节点,并作为参数node的孩子。 */
    DefaultMutableTreeNode treeNode;
    TreeNodeObject tno;
    String curParentId;
    String curNodeId;
     for( int i = 0; i < treeDataList.size(); i++){
      tno = treeDataList.get(i);
      curParentId = tno.getParentId();
      curNodeId =tno.getNodeId();
       if(!curParentId.equals(nodeId)) continue;
      treeNode = new DefaultMutableTreeNode();
      treeNode.setUserObject(tno);
      node.add(treeNode);
      jtree.updateUI();
      addChildren(treeNode, curNodeId);
    }
  }
   /**
    * 给JTree添加鼠标事件
    * @param jtree JTree
    */
   public void addClickEvent( final JTree jtree){
    jtree.addMouseListener( new MouseListener(){
       public void mouseClicked(MouseEvent e) {
         /* 当鼠标双击事件被触发时,调用alert方法显示树节点的userObject值。 */
         int row = jtree.getRowForLocation(e.getX(), e.getY());
         if(row == -1) return;
         if(e.getClickCount() == 1) return;
        DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode)jtree.getLastSelectedPathComponent();
        TreeNodeObject tno = (TreeNodeObject)treeNode.getUserObject();
        alert(tno.toString2());
      }
       public void mouseEntered(MouseEvent e) {}
       public void mouseExited(MouseEvent e) {}
       public void mousePressed(MouseEvent e) {}
       public void mouseReleased(MouseEvent e) {}
    });
  }
   /**
    * 在消息对话框中显示信息
    * @param msg String
    */
   public void alert(String msg) {
    JOptionPane.showMessageDialog( null, msg, "提示",  JOptionPane.INFORMATION_MESSAGE);
  }
   /**
    * Get JTree
    * @return JTree
    */
   public JTree getJTree(){
     return jtree;
  }
   /**
    * 从数据库中获取数据存储在TreeNodeObject类的对象列表中
    * @return List
    */
   public List<TreeNodeObject> getTreeData(){
    List<TreeNodeObject> list = new LinkedList<TreeNodeObject>();
    TreeNodeObject tno;
    String sql = "select * from userprivilege t where userid = '14'";
    Connection conn = connection;
     try{
      Statement stmt = conn.createStatement();
      ResultSet rs = stmt.executeQuery(sql);
       while(rs.next()){
        tno = new TreeNodeObject();
        tno.setNodeId(rs.getString( "MODELID"));
        tno.setParentId(rs.getString( "PARENTID"));
        tno.setNodeName(rs.getString( "MODELNAME"));
        list.add(tno);
      }
      rs.close();
      stmt.close();
    } catch(SQLException e){
      e.printStackTrace();
    }
     return list;
  }
   /**
    * 入口方法
    * @param args String[]
    */
   public static void main(String[] args){
     new MyTree();
  }
}
 
MyTree使用:
    jtree = new MyTree().getJTree();
    jp.setLayout( new BorderLayout());
    jp.add( new JScrollPane(jtree));
 
展示界面见附件。

你可能感兴趣的:(职场,树,休闲,双击事件)