如何在Jtree中刷新XML

时间:2021-04-20 12:33:50

I read here , but if the xml file changes the jtree does not reload /refreshes
how to create a function for refresh / reload Jtree
I try to write code :

我在这里阅读,但是如果xml文件改变了jtree没有重新加载/刷新如何创建刷新/重载Jtree的函数我尝试编写代码:

refreshAction = new AbstractAction("Refresh", IconFactory.getIcon("delete", IconFactory.IconSize.SIZE_16X16)) {
public void actionPerformed(ActionEvent e) {
    XMLTree xmlClass = null;
    ((DefaultTreeModel) xmlClass.getModel()).reload(); 
    System.out.println("Refresh");
}};

but i got the error : java.lang.NullPointerException

但我得到了错误:java.lang.NullPointerException

3 个解决方案

#1


3  

I added a new Action to popup in getJPopupForExplorerTree(). You'll probably want to re-factor xmlFile out of the XMLTree constructor; I've hard coded it for expedience below:

我在getJPopupForExplorerTree()中添加了一个新的Action来弹出。你可能想要从XMLTree构造函数中重新考虑xmlFile;我在下面为了方便而硬编码:

popup.add(new AbstractAction("Reload") {

    public void actionPerformed(ActionEvent e) {
        System.out.println("Reload");
        try {
            root = getRoot("xml.xml");
            setModel(new XMLTreeModel(root));
        } catch (Exception ex) {
            ex.printStackTrace(System.err);
        }
    }
});

#2


2  

  • this is most complex code, probably

    这可能是最复杂的代码

  • read tutorial about JTables DefaultTableModel (good described concept and logics for DefaultXxxModel is similair / the same)

    阅读关于JTables DefaultTableModel的教程(对于DefaultXxxModel,描述的概念和逻辑是类似的/相同的)

  • read tutorial about JTree

    阅读关于JTree的教程

  • read tutorial about Concurency in Swing,

    阅读有关Swing中Concurency的教程,

  • especially description about SwingWorker

    特别是关于SwingWorker的描述

  • in your case better (sorry for that) would be create an new instance for DefaultTreeModel, fills data by using SwingWorker, add new model to the visible JTree,

    在你的情况下更好(抱歉)将为DefaultTreeModel创建一个新实例,使用SwingWorker填充数据,将新模型添加到可见的JTree,

  • by replacing model you'll lost all changes in the current JTree

    通过替换模型,您将丢失当前JTree中的所有更改

#3


1  

I dont know the spesific code but you can try this

我不知道特定代码,但你可以试试这个

refreshAction = new AbstractAction("Refresh", IconFactory.getIcon("delete", IconFactory.IconSize.SIZE_16X16)) {
public void actionPerformed(ActionEvent e) {
     DefaultTreeModel myTreeModel = (DefaultTreeModel) xmlClass.getModel();

     myTreeModel.reload();

     revalidate();
     repaint();
}}; 

#1


3  

I added a new Action to popup in getJPopupForExplorerTree(). You'll probably want to re-factor xmlFile out of the XMLTree constructor; I've hard coded it for expedience below:

我在getJPopupForExplorerTree()中添加了一个新的Action来弹出。你可能想要从XMLTree构造函数中重新考虑xmlFile;我在下面为了方便而硬编码:

popup.add(new AbstractAction("Reload") {

    public void actionPerformed(ActionEvent e) {
        System.out.println("Reload");
        try {
            root = getRoot("xml.xml");
            setModel(new XMLTreeModel(root));
        } catch (Exception ex) {
            ex.printStackTrace(System.err);
        }
    }
});

#2


2  

  • this is most complex code, probably

    这可能是最复杂的代码

  • read tutorial about JTables DefaultTableModel (good described concept and logics for DefaultXxxModel is similair / the same)

    阅读关于JTables DefaultTableModel的教程(对于DefaultXxxModel,描述的概念和逻辑是类似的/相同的)

  • read tutorial about JTree

    阅读关于JTree的教程

  • read tutorial about Concurency in Swing,

    阅读有关Swing中Concurency的教程,

  • especially description about SwingWorker

    特别是关于SwingWorker的描述

  • in your case better (sorry for that) would be create an new instance for DefaultTreeModel, fills data by using SwingWorker, add new model to the visible JTree,

    在你的情况下更好(抱歉)将为DefaultTreeModel创建一个新实例,使用SwingWorker填充数据,将新模型添加到可见的JTree,

  • by replacing model you'll lost all changes in the current JTree

    通过替换模型,您将丢失当前JTree中的所有更改

#3


1  

I dont know the spesific code but you can try this

我不知道特定代码,但你可以试试这个

refreshAction = new AbstractAction("Refresh", IconFactory.getIcon("delete", IconFactory.IconSize.SIZE_16X16)) {
public void actionPerformed(ActionEvent e) {
     DefaultTreeModel myTreeModel = (DefaultTreeModel) xmlClass.getModel();

     myTreeModel.reload();

     revalidate();
     repaint();
}};