0

在我的 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.
4

0 回答 0