当前位置: 首页 > 学习 > 电脑学习 > 程序设计 > JAVA > APPLET与SWING > 正文

使用jdom操作xml数据,生成含Jtree的applet

http://www.zk168.com.cn  招考学习网 2006-4-11 1:16:17
-----------------------------------------------------------[交流]-[打印]-[发送]-[收藏]--
在我们工作中,常常会碰到树形组件的生成问题,如果你在开发web application,纯粹使用javascript来生成树形组件是非常繁琐的,而且交互性也不不太好。所以许多产品使applet来实现树形组件的功能。比如说,weblogic,jboss等产品的console.所以,把树形数据组织成xml文件,用jdom剖析它,最后生成applet就非常有通用的意义。下面,我就给出一个例子,抛砖引玉。

· 1.准备一个存有属性数据的xml文件,把它放在classpath中,我这里是org.xml。

<?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>

· 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函数,达到刷新页面的目的。
-----------------------------------------------------------[交流]-[打印]-[发送]-[收藏]--
最新入库:
 
·实质、过程及意义——阿多尔诺“否定的辩证法”探微
·从Ontology的译名之争看哲学术语的翻译原则
·论马克思主义哲学经典的解释——解释学方法及其在马克
·中国哲学当前的核心与周边问题
·和合学与21世纪文化价值和科技
·中国文化的和合精神与21世纪
·宗教之间理当相互宽容
·上半个世纪的自由主义
·殷周至春秋时期神人关系之演进
·大学之道:构建以“三纲八目”为核心的道德修养体系
相关内容:
 
·环保企业人力资源开发与管理的实证研究————巨龙公
·重油制气污水处理系统(A/O)技术改造
·英美CPA管理模式及其启示
·改造NERA微波公务信道为国产监控信道
·EAStudio让电子商务网站如虎添翼
·基于PB6和ORACLE8开发“劳动信息管理系统”
·巧解Pretty  Park 病毒一例
·CDMA在中国的应用以及向CDMA2000的过度中的问题研究
·SMA施工控制与现场监理
·浅谈改性沥青及SMA路面平整度的控制
网友点评:
 
会员名称:
密码:匿名 ·注册·忘记密码?
评论内容:
(最多300个字符)
  查看评论
友情提醒:
 1.库中的资料大都来自互联网、网友上传、各类书籍,在录入的过程中难免会出现错误,恳请网
 友来信指正!
 2.如果网友在本库中未能找到所需要的材料,请登陆到我们的论坛《招考学习网》版块!
 3.考友想加入招考学习网的编辑部,请发信到XueXiWang#Gmail.com(#改为@)附带个人简历
 4.如需转载请注明出处及作者,谢谢合作!
 5.如果您有更好的建议或意见请EMAIL:XueXiWang#Gmail.com (#改为@)
 6.凡标题中有注有“[NO]”字样均不含答案且答案整理中.
 7.如本库中转载文章涉及版权等问题,请相关网站或作者在两周内发邮件通知(EMAIL:  XueXiWang#Gmail.com (#改为@))我们,我们接到通知后立即删除该文章及链接!
你问我答 更多>>