JavaSwing实现树形结构

一、Applet

package com.jy.applet.day1206;

import java.awt.Container;

import javax.swing.JApplet;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.jy.applet.day1206.swing.pane.TestPane;



/**
 * 用来实现树形结构的Applet
 * 
 * @author Administrator
 * 
 */
@SuppressWarnings("serial")
public class AppletTest1 extends JApplet {
	private static final Log log = LogFactory.getLog(AppletTest1.class);// 日志记录
	private static AppletTest1 instance;
	public static Container container;// 一个可包含其他 AWT 组件的组件。

	public void init() {
		log.info("Applet初始化......");
		instance = this;
		SwingUtilities.invokeLater(new Runnable() {

			public void run() {
				try {
					UIManager.setLookAndFeel(UIManager
							.getSystemLookAndFeelClassName());//设置外观感觉
					container = getContentPane();//获取内容面板
					TestPane pane = new TestPane();//初始化主面板
					container.add(pane);//将主面板添加到内容面板
				} catch (Exception e) {
					e.printStackTrace();
				}

			}

		});
	}

	public static AppletTest1 getInstance() {
		return instance;
	}

	public void destroy() {
		log.info("程序正在关闭.....");
	}

}

二、主面板

package com.jy.applet.day1206.swing.pane;

import java.util.List;

import javax.swing.JScrollPane;
import javax.swing.JTree;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.jy.applet.day1206.swing.tree.JTreeNode;
import com.jy.applet.util.DataTestDao;



@SuppressWarnings({"serial","unchecked"})
public class TestPane extends JScrollPane{
	private static final Log log = LogFactory.getLog(TestPane.class);
	private static TestPane instance;//实例变量
	public JTree myTree;//面板中的树
	JTreeNode rootNode ;//自定义根节点
	public TestPane(){
		instance = this;
		init();
	}
	
	private void init() {
		log.info("开始初始化主面板......");
		setAutoscrolls(true);
		getViewport().removeAll();
		rootNode = new JTreeNode("中国");
		JTreeNode secondTreeNode = null;//第二层节点
		JTreeNode leafTreeNode = null;//叶子节点
		List provinceList = DataTestDao.getProvinceList();//省份的集合
		String provinceId = null;
		/**如果省份和城市的集合不为空,并且元素个数大于0**/
		if (provinceList != null && provinceList.size() > 0) {
			for (int i = 0; i < provinceList.size(); i++) {
				secondTreeNode = (JTreeNode) provinceList.get(i);//获取二层节点
				provinceId = secondTreeNode.getId();//获取省份ID
				List cityList = DataTestDao.getCityListByProID(provinceId);//根据省份ID查询城市
				if(cityList != null && cityList.size() > 0){
					for(int j = 0;j < cityList.size();j ++){
						leafTreeNode = cityList.get(j);//获取叶子节点,即城市
						secondTreeNode.add(leafTreeNode);//将叶子节点挂到第二层节点上
					}
				}
				rootNode.add(secondTreeNode);//把第2层节点,添加到根节点 
			}
		}
		
		myTree = new JTree(rootNode);//以rootNode为根节点,创建一个树形结构
		myTree.putClientProperty("JTree.lineStyle", "Horizontal");//树形结构的线条风格
		myTree.setVisible(true);
		myTree.updateUI();
		instance.setViewportView(myTree);
                /**注册事件**/
                MyTreeAdapter treeAdapter = new MyTreeAdapter();
                myTree.addTreeSelectionListener(treeAdapter);
 }
	public static TestPane getInstance() {
		return instance;
	}
}

三、用来模拟数据库的DAO

package com.jy.applet.util;

import java.util.ArrayList;
import java.util.List;

import com.jy.applet.day1206.swing.tree.JTreeNode;
import com.jy.applet.entity.City;

/**
 * 用户模拟数据的DAO
 * @author Administrator
 *
 */
@SuppressWarnings("unchecked")
public class DataTestDao {

	public static List getProvinceList() {
		List provinces = new ArrayList();
		provinces.add("北京市");
		provinces.add("天津市");
		provinces.add("上海市");
		provinces.add("重庆市");
		provinces.add("山东省");
		provinces.add("江苏省");
		provinces.add("浙江省");
		provinces.add("广东省");
		
		List returnList = new ArrayList();
		JTreeNode treeNode = null;
		for(int i=0;i < provinces.size();i++){
			treeNode = new JTreeNode(provinces.get(i));
			treeNode.setId(i+"");
			returnList.add(treeNode);
		}
		return returnList;
	}

	public static List getCityListByProID(String provinceId) {
		List citys = new ArrayList();
		List returnList = new ArrayList();
		JTreeNode treeNode = null;
		if(provinceId.equals("0")){
			//北京市
			citys.add(new City(provinceId,"海淀区"));
			citys.add(new City(provinceId,"东城区"));
			citys.add(new City(provinceId,"西城区"));
		}else if(provinceId.equals("1")){
			//天津市
			citys.add(new City(provinceId,"河东区"));
			citys.add(new City(provinceId,"河西区"));
			citys.add(new City(provinceId,"塘沽区"));
		}else if(provinceId.equals("2")){
			//上海市
			citys.add(new City(provinceId,"浦东新区"));
			citys.add(new City(provinceId,"黄浦区"));
			citys.add(new City(provinceId,"宝山区"));
		}else if(provinceId.equals("3")){
			//重庆市
			citys.add(new City(provinceId,"渝北区"));
			citys.add(new City(provinceId,"巫山县"));
			citys.add(new City(provinceId,"涪陵区"));
		}else if(provinceId.equals("4")){
			//山东省
			citys.add(new City(provinceId,"济南市"));
			citys.add(new City(provinceId,"青岛市"));
			citys.add(new City(provinceId,"烟台市"));
			citys.add(new City(provinceId,"潍坊市"));
			citys.add(new City(provinceId,"日照市"));
			citys.add(new City(provinceId,"聊城市"));
		}else if(provinceId.equals("5")){
			//江苏省
			citys.add(new City(provinceId,"南京市"));
			citys.add(new City(provinceId,"徐州市"));
			citys.add(new City(provinceId,"苏州市"));
			citys.add(new City(provinceId,"无锡市"));
			citys.add(new City(provinceId,"镇江市"));
			citys.add(new City(provinceId,"扬州市"));
		}else if(provinceId.equals("6")){
			//浙江省
			citys.add(new City(provinceId,"杭州市"));
			citys.add(new City(provinceId,"温州市"));
			citys.add(new City(provinceId,"宁波市"));
			citys.add(new City(provinceId,"绍兴市"));
			citys.add(new City(provinceId,"金华市"));
			citys.add(new City(provinceId,"衢州市"));
		}else if(provinceId.equals("7")){
			//广东省
			citys.add(new City(provinceId,"广州市"));
			citys.add(new City(provinceId,"深圳市"));
			citys.add(new City(provinceId,"珠海市"));
			citys.add(new City(provinceId,"东莞市"));
			citys.add(new City(provinceId,"惠州市"));
			citys.add(new City(provinceId,"肇庆市"));
		}else{
			
		}
		for(int i=0;i < citys.size();i++){
			treeNode = new JTreeNode(citys.get(i).getName());
			treeNode.setId(i+"");
			returnList.add(treeNode);
		}
		return returnList;
	}

}

四、监听器

package com.jy.applet.day1206.swing.adapter;

import javax.swing.JOptionPane;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreePath;

import com.jy.applet.day1206.swing.tree.JTreeNode;

public class MyTreeAdapter implements TreeSelectionListener {

	@Override
	public void valueChanged(TreeSelectionEvent e) {
		TreePath treePath = e.getNewLeadSelectionPath();// 例如[中国, 江苏省, 南京市]
		if (treePath != null) {
			// 返回路径上最底层的组件,对于树形结构,即叶子节点
			// this will return an instance of TreeNode.
			JTreeNode node = (JTreeNode) treePath.getLastPathComponent();
			StringBuilder sb = new StringBuilder();
			sb.append("您选择的是:");
			sb.append(node);
			sb.append("\r\n");
			sb.append("路径为:");
			sb.append(treePath);
			JOptionPane.showMessageDialog(null, sb, "提示",
					JOptionPane.INFORMATION_MESSAGE);
		} else {
			JOptionPane.showMessageDialog(null, "您尚未选择省份或城市!", "错误",
					JOptionPane.ERROR_MESSAGE);
		}
	}

}

五【效果如下】

JavaSwing实现树形结构_第1张图片 JavaSwing实现树形结构_第2张图片

你可能感兴趣的:(Java基础)