Use JDM to operate XML data, generate AppletJagie original (participating: 291, expert points: 1430) Published: 2003-11-5 4:06 PM: 2003-11-5 4:22 PM: 1.0 Read: 3608 times http://www.javaresearch.org/Article/showArticle.jsp?column=287&thread=10150
Keywords: XML, JDOM, Applet, JTree In our work, often encountering the generation of tree components, if you develop Web Application, purely using JavaScript to generate tree components is very cumbersome, and interactivity is also 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] XML Version = "1.0" encoding = "UTF-8"?> < 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 =" "" "#"> "#">
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 (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, the file resolves the XML file, generates an applet containing JTREE, you can embed it into the JSP, use it 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 ColorSource (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 there is a parent node Element Father = E.GETParent (); if (Father! = null) {string fid = Father.gettributeValue ("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 = () nnext (); addnode (child, rootnode);}} / ** * According to ID, find tree nodes, // breadth priority * @PARAM ID Node ID * @Param rootNode 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;