使用jdom操作xml数据,生成含Jtree的applet
转自:http://www.softhouse.com.cn/html/200410/2004102517145700001335.html
使用jdom操作xml数据,生成含Jtree的applet
在我们工作中,常常会碰到树形组件的生成问题,如果你在开发web application,纯粹使用javascript来生成树形组件是非常繁琐的,而且交互性也不不太好。所以许多产品使applet来实现树形组件的功能。比如说,weblogic,jboss等产品的console.所以,把树形数据组织成xml文件,用jdom剖析它,最后生成applet就非常有通用的意义。下面,我就给出一个例子,抛砖引玉。1.准备一个存有属性数据的xml文件,把它放在classpath中,我这里是org.xml。[pre]<?xml version="1.0" encoding="UTF-8"?><!--Sample XML file generated by XMLSPY v5 rel. 3 U (http://www.xmlspy.com)--><node xmlns="http://www.javabox.com/schemas/org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.javabox.com/schemas/orgE:\myDemo\org.xsd" name="组织机构" id="-1" desc="" link="#"> <node name="总经理" id="1" desc="" link="#"> <node name="管理副总经理" id="2" desc="" link="#"/> <node name="生产副总经理" id="3" desc="" link="#"> <node name="项目部" id="7" desc="" link="#"/> <node name="机械公司" id="8" desc="" link="#"/> <node name="贝盟公司" id="9" desc="" link="#"/> <node name="洛斯韦公司" id="9" desc="" link="#"/> </node> <node name="总工程师" id="4" desc="" link="#"/> <node name="总会计师" id="5" desc="" link="#"/> <node name="总经济师" id="6" desc="" link="#"/> </node></node>[/pre]2.确保你可以使用jdom解析器,你如果没有可以去这里下载。3.用于代表树结点节点的javabean,TreeNode.javapackage com.javabox.jtree;public class TreeNode{ private String id; private String name; private String link; public TreeNode(String id,String name,String link){ this.id=id; this.name=name; this.link=link; } public String getId(){ return id; } public void setId(String Id){ this.id=Id; } public void setName(String Name){ this.name=Name; } public String getName(){ return name; } public String toString(){ return name; } public String getLink(){ return link; } public void setLink(String link){ this.link=link; }}4.自己写的TreeCellRenderer,IconRender.javapackage com.javabox.jtree;import javax.swing.*;import java.awt.*;import javax.swing.tree.*;import javax.swing.tree.DefaultTreeCellRenderer;class IconRender extends DefaultTreeCellRenderer { //你需要替换成你的icon public static final Icon leafSelectedIcon = new ImageIcon("greeball.JPG"); public static final Icon leafUnSelectedIcon = new ImageIcon("greyball.JPG"); public static final Icon folderOpen = new ImageIcon("folderopen.JPG"); public static final Icon folderClose = new ImageIcon("folderclose.JPG"); public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,  
推荐文章 |
