项目需求,从数据库动态生成JTree。
有两种方式:1,在树里边定义一个list<Department> child的字段
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.sc.po; import java.util.ArrayList; import java.util.List; /** * * @author syj */ public class Department { private int id; private String dname; private int pid; private String comment; private List<Department> children=new ArrayList<Department>(); /** * @return the id */ public int getId() { return id; } /** * @param id the id to set */ public void setId(int id) { this.id = id; } /** * @return the dname */ public String getDname() { return dname; } /** * @param dname the dname to set */ public void setDname(String dname) { this.dname = dname; } /** * @return the pid */ public int getPid() { return pid; } /** * @param pid the pid to set */ public void setPid(int pid) { this.pid = pid; } /** * @return the comment */ public String getComment() { return comment; } /** * @param comment the comment to set */ public void setComment(String comment) { this.comment = comment; } /** * @return the children */ public List<Department> getChildren() { return children; } /** * @param children the children to set */ public void setChildren(List<Department> children) { this.children = children; } }数据库
CREATE TABLE `department` ( `id` int(11) NOT NULL AUTO_INCREMENT, `dname` varchar(36) NOT NULL, `pid` int(11) NOT NULL, `comment` varchar(150) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8;
public List<Department> getDepartmentsByPid(int pid)//通过pid构建成树的list集合 { List<Department> resultlist=new ArrayList<Department>(); List<Department> departmentlist=departmentdao.getDepartmentByPid(pid);//通过父节点获得子节点 for(Department department:departmentlist) { List<Department> children=getDepartmentsByPid(department.getId()); department.setChildren(children); resultlist.add(department); } return resultlist; };
构建树:
public static void initTree(List<Department> list, DefaultMutableTreeNode parent) { for (Department department : list) { List<Department> children = department.getChildren(); DefaultMutableTreeNode other = new DefaultMutableTreeNode(new KeyValue( department.getDname(), department)); initTree(children, other); parent.add(other); } }
DepartmentService departmentService = new DepartmentServiceImpl(); Department department = departmentService.getDepatmentById(1); DefaultMutableTreeNode root = new DefaultMutableTreeNode(new KeyValue( department.getDname(), department)); List<Department> list1 = departmentService.getDepartmentsByPid(department.getId()); initTree(list1,root); // List<Department> list = departmentService.getAllDepatment(); // initTree1(list, 1, root); DefaultTreeModel model = new DefaultTreeModel(root); jTree1.setModel(model); jTree1.getSelectionModel().setSelectionMode( DefaultTreeSelectionModel.SINGLE_TREE_SELECTION);
数据库获取Dapartment表中的所有字段
service
public List<Department> getAllDepatment() { List<Department> list=departmentdao.getAllDepatment(); list.remove(0); return list; }
生成树
public static void initTree1(List<Department> list, int pid, DefaultMutableTreeNode parent) { for (Department department : list) { if (department.getPid() == pid) { DefaultMutableTreeNode other = new DefaultMutableTreeNode(new KeyValue( department.getDname(), department)); initTree1(list, department.getId(), other); parent.add(other); } } }
显示数
DepartmentService departmentService = new DepartmentServiceImpl(); Department department = departmentService.getDepatmentById(1); DefaultMutableTreeNode root = new DefaultMutableTreeNode(new KeyValue( department.getDname(), department)); // List<Department> list1 = departmentService.getDepartmentsByPid(department.getId()); // initTree(list1,root); List<Department> list = departmentService.getAllDepatment(); initTree1(list, 1, root); DefaultTreeModel model = new DefaultTreeModel(root); jTree1.setModel(model); jTree1.getSelectionModel().setSelectionMode( DefaultTreeSelectionModel.SINGLE_TREE_SELECTION);
截图:两种结果都一样