在我的GUI中,我必须逐个按照JTree结构加载多个XML文件,如果我想通过右键单击编辑或删除完整的JTree或值,我该如何有效地执行此操作?任何线索?
我将xml文件处理为jtree,如下所示,
/* Tree implementation. */
xmlTree = new JTree();
xmlTree.setRootVisible(false);
xmlTree.setEditable(true);
scrollPane.setViewportView(xmlTree);
frame.getContentPane().add("Center", scrollPane);
+++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++
tn = processElement(xmlDoc.getRootElement());
((DefaultTreeModel) xmlTree.getModel()).setRoot(tn);
+++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++
// Open XMl file into the GUI.
private DefaultMutableTreeNode processElement(Element el) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(el.getName());
String text = el.getTextNormalize();
if ((text != null) && (!text.equals(""))) {
node.add(new DefaultMutableTreeNode(text));
}
processAttributes(el, node);
Iterator<Element> children = el.getChildren().iterator();
while (children.hasNext()) {
Element next = (Element) children.next();
node.add(processElement(next));
} // End of while loop.
return node;
} // End of method.
// Processing Element attributes.
private void processAttributes(Element el, DefaultMutableTreeNode dmtn) {
Iterator<Attribute> atts = el.getAttributes().iterator();
while (atts.hasNext()) {
Attribute att = (Attribute) atts.next();
DefaultMutableTreeNode attNode = new DefaultMutableTreeNode("@" + att.getName());
attNode.add(new DefaultMutableTreeNode(att.getValue()));
dmtn.add(attNode);
} // End of while loop.
} // End of method.