删除一些节点后,JTree模型会被更新

时间:2014-05-06 08:56:47

标签: java xml swing jtree

我坚持使用标准的JTree默认模型。这就是重点。

我创建了某种XML编辑器,我有一个JTree组件和一个JTextPane组件。我可以使用我的GUI删除JTree中的节点,代码在这里:

DefaultTreeModel model = MyGUI.treemodel;    // this is a model of my jtree
for (int i=0; i<arrayCheckboxes.size(); i++) {    // I am using checkboxes for selecting nodes to delete
    if (arrayCheckboxes.get(i).isSelected()) {
        model.removeNodeFromParent((MutableTreeNode) thisnode.getChildAt(i+2));    // first two childs should be kept
    }
}
SwingUtilities.updateComponentTreeUI(MyGUI.tree);  // refreshing UI

所以,一切都很好,我可以在JTree中看到节点已被删除。

在那之后我将新的JTree导出到XML文件,代码在这里:

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ConvertToXML {

    public static void convert() {

        try {

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            Document doc = factory.newDocumentBuilder().newDocument();
            Element rootElement = doc.createElement("JOB");

            doc.appendChild(rootElement);

            // Get tree root...
            DefaultMutableTreeNode root = (DefaultMutableTreeNode) MyGUI.treemodel.getRoot();
            System.out.println("model in convert: " + MyGUI.treemodel);
            Node node = (Node) root.getUserObject();

            for (int i = 0; i < node.getAttributes().getLength() - 1; i++) {
                rootElement.setAttribute(node.getAttributes().item(i).getNodeName(), node.getAttributes().item(i).getNodeValue());
            }

            parseTreeNode(node.getChildNodes(), rootElement);

            // Save the document to disk...

            Transformer tf = TransformerFactory.newInstance().newTransformer();
            tf.setOutputProperty(OutputKeys.INDENT, "yes");
            tf.setOutputProperty(OutputKeys.METHOD, "xml");
            tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

            JFileChooser jFileChooser = new JFileChooser();
            jFileChooser.setDialogTitle("Укажите файл для сохранения");
            int userSelection = jFileChooser.showSaveDialog(MyGUI.frame);
            File fileToSave = null;
            if (userSelection == JFileChooser.APPROVE_OPTION) {
                fileToSave = jFileChooser.getSelectedFile();
                System.out.println("Сохранить как: " + fileToSave.getAbsolutePath());

            }


            DOMSource domSource = new DOMSource(doc);
            //StreamResult sr = new StreamResult(new File("TreeModel.xml"));
            StreamResult sr = new StreamResult(fileToSave);
            tf.transform(domSource, sr);

        } catch (ParserConfigurationException | TransformerException ex) {
            ex.printStackTrace();
        }
    }


    private static void parseTreeNode(NodeList nodeList, Node doc) {
        String nameNode;
        for (int count = 0; count < nodeList.getLength(); count++) {
            Element parentElement = null;
            Node tempNode = nodeList.item(count);

            if ((tempNode.getNodeType() == Node.ELEMENT_NODE)) {
                nameNode = tempNode.getNodeName();
                parentElement = doc.getOwnerDocument().createElement(nameNode);
                doc.appendChild(parentElement);
                for (int i = 0; i < tempNode.getAttributes().getLength(); i++) {
                    parentElement.setAttribute(tempNode.getAttributes().item(i).getNodeName(), tempNode.getAttributes().item(i).getNodeValue());
                }
            }
            if (tempNode.hasChildNodes()) {
                parseTreeNode(tempNode.getChildNodes(), parentElement);
            }
        }
    }
}

我得到了什么?我的新XML文件没有任何更改,以便与原始文件进行比较,似乎我没有删除模型中的任何内容。但为什么会这样呢? 请帮助我,我完全陷入了这个错误。

0 个答案:

没有答案