Use the JDOM to operate XML data to generate an applet containing JTREE

xiaoxiao2021-03-06  14

Transfer from: http://www.softhouse.com.cn/html/200410/2004102517145700001335.html

Use the JDOM to operate XML data to generate an applet containing JTREE

In our work, you often touch the generation of tree components, if you develop Web Application, purely using JavaScript to generate tree components is very cumbersome, and interactivity is not very good. So many products enable Applet to implement the function of the tree component. For example, WebLogic, JBoss and other products console. So, organize tree data into XML files, analyze it with JDom, and finally generate Applets very common meaning. Below, I will give an example and throw the brick. 1. Prepare an XML file with attribute data, put it in classpath, I am org.xml.

[pre] < 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 =" Organization "ID =" - 1 "DESC =" "" "#"> "#"> [/ pre] 2. Make sure you can use the JDOM parser, if you don't go here

download. 3. Representative javabean tree node for the node, 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. Written TreecellRenderer, iconrender.javaPackage Com.javabox.jtree; import javax.swing. *; import java.aw. *; import javax.swing.tree. *; Import javax.swing.tree .DefaultTreecellrenderer; Class iconrender Extends defaulttreecellrenderer {// You need to replace your 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 (IconRender.leafselected);} else if (leaf) {seticon (iconrender.leafunselectedico) n);} Return this;} public iconrender () {super (); this.setleaficon; this.setopenicon; this.setClosedicon (FolderClose);}} 5.applettree.java, this file parsing XML File, generate an applet containing JTREE, you can embed it into JSP, in the HTML file, or you can run the file directly.

Package com.javabox.jtree; import javax.swing.event. *; import java.awt. *; import java.applet. *; import javax.swing. *; import javax.swing.tree. *; import java.Awt it. *; 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"); TR ee.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 ("ID"), root.getattributevalue ("name"), root.getattributevalue ("link"); top = new DefaultmutableTreenode (rootnode);

Addnode (root, top);} catch (exception ex) {ex.printstacktrace ();} // You can change the color of JTREE in JTREE, I ask the foreign master to find it, cool :) UIManager.put ("tree.hash", new coloruiresource (color.red); return new jtree (top);

/ ** * * @Param E to join the JDOM element of the tree * @Param rootnode tree root node * /

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); // If you have a parent node

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));}} // If there is a child node

Iterator it = E.Getchildren (). Iterator (); while (it.hasnext ()) {element child = (element) it.next (); addnode (child, rootnode);}}

/ ** * According to ID, find tree nodes, // Scene priority * @Param ID node ID * @Param rootnode tree root node * @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 ();} // call a JavaScript function; // 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. Running a class, is it very cool, you can also embed it in a web page, call the JavaSript function to achieve the purpose of refresh the page.

转载请注明原文地址:https://www.9cbs.com/read-45817.html

New Post(0)