使用jdom操作xml数据,生成含Jtree的applet
Jagie 原创 (参与分:291,专家分:1430) 发表:2003-11-5 下午4:06 更新:2003-11-5 下午4:22 版本:1.0 阅读:3608次
http://www.javaresearch.org/article/showarticle.jsp?column=287&thread=10150
关键词:xml,jdom,applet,jtree
在我们工作中,常常会碰到树形组件的生成问题,如果你在开发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/org
E:\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.java
package 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.java
package 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,
boolean leaf,
int row,
boolean hasFocus) {
super.getTreeCellRendererComponent(tree, value, selected, expanded,
leaf, row, hasFocus);
if (leaf && selected) {
setIcon(IconRender.leafSelectedIcon);
}
else if (leaf) {
setIcon(IconRender.leafUnSelectedIcon);
}
return this;
}
public IconRender() {
super();
this.setLeafIcon(leafUnSelectedIcon);
this.setOpenIcon(folderOpen);
this.setClosedIcon(folderClose);
}
}
5.AppletTree.java,该文件解析xml文件,生成含Jtree的applet,你可以把它嵌入到jsp,html文件中使用,也可以直接运行该文件。
package com.javabox.jtree;
import javax.swing.event.*;
import java.awt.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.event.*;
import org.jdom.*;
import org.jdom.input.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.plaf.*;
import javax.swing.plaf.basic.*;
import javax.swing.plaf.metal.*;
import java.io.*;
import netscape.javascript.*;
public class AppletTree extends Applet implements TreeSelectionListener
{
private JTree tree;
private TreePath path;
private Panel topPanel;
private DefaultMutableTreeNode top;
private DefaultMutableTreeNode clicknode;
private String link;
public AppletTree(){
}
public void init(){
try{
super.init();
this.setLayout(new GridLayout(1,1));
tree=createTree(new FileInputStream("org.xml"));
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.putClientProperty("JTree.lineStyle","Angled");
tree.setShowsRootHandles(true);
tree.setEditable(false);
tree.addTreeSelectionListener( this );
IconRender render=new IconRender();
tree.setCellRenderer(render);
topPanel=new Panel(new BorderLayout());
topPanel.add(tree);
this.add(topPanel);
}catch(Exception e){
e.printStackTrace();
}
}
public JTree createTree(InputStream is){
SAXBuilder builder = new SAXBuilder();
try {
Document doc = builder.build(is);
Element root=doc.getRootElement();
TreeNode rootNode=new
TreeNode(root.getAttributeValue("id"),root.getAttributeValue("name"),root.getAttributeValue("link"));
top=new DefaultMutableTreeNode(rootNode);
addNode(root,top);
} catch (Exception ex) {
ex.printStackTrace();
}
//你可以在这里改变jtree中连线的颜色,我请教国外的高手才找到的,很酷的哦:)
UIManager.put( "Tree.hash", new ColorUIResource(Color.red) );
return new JTree(top);
}
/**
*
* @param e 待加入树种的jdom元素
* @param rootNode 树根节点
*/
private void addNode(Element e,DefaultMutableTreeNode rootNode){
String id=e.getAttributeValue("id");
String name=e.getAttributeValue("name");
String link=e.getAttributeValue("link");
TreeNode node=new TreeNode(id,name,link);
//如有父节点
Element father=e.getParent();
if(father!=null){
String fid=father.getAttributeValue("id");
DefaultMutableTreeNode fatherNode=getTreeNode(fid,rootNode);
if(fatherNode!=null){
fatherNode.add(new DefaultMutableTreeNode(node));
}
}
//如有子节点
Iterator it=e.getChildren().iterator();
while(it.hasNext()){
Element child=(Element)it.next();
addNode(child,rootNode);
}
}
/**
* 根据id,查找树节点,//广度优先
* @param id 节点id
* @param rootNode 树根节点
* @return DefaultMutableTreeNode
*/
private DefaultMutableTreeNode getTreeNode(String id,DefaultMutableTreeNode rootNode){
DefaultMutableTreeNode returnNode=null;
if(rootNode!=null){
Enumeration enum=rootNode.breadthFirstEnumeration();
while(enum.hasMoreElements()){
DefaultMutableTreeNode temp=(DefaultMutableTreeNode)enum.nextElement();
TreeNode node=(TreeNode)temp.getUserObject();
if(node.getId().equals(id)){
returnNode=temp;
break;
}
}
}
return returnNode;
}
public void valueChanged( TreeSelectionEvent event ){
if( event.getSource() == tree ){
path = event.getPath();
clicknode=(DefaultMutableTreeNode)path.getLastPathComponent();
Object uo=clicknode.getUserObject();
if(uo instanceof TreeNode){
TreeNode nd=(TreeNode)clicknode.getUserObject();
link=nd.getLink();
}
//调用一个javascript函数;
// JSObject.getWindow (this).eval ("javascript:window.open('"+link+"')") ;
}
}
public static void main(String[] args ){
JFrame frame=new JFrame("test");
AppletTree tree=new AppletTree();
tree.init();
frame.getContentPane().add(tree);
frame.setSize(600,600);
frame.show();
}
}
6.运行一个这个类,是不是很cool哦,你还可以把它嵌在网页中,调用javasript函数,达到刷新页面的目的。
希望该文章能对你有所启发,祝你成功!