How to refresh XML in Jtree

删除回忆录丶 提交于 2019-11-28 14:05:33

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:

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);
        }
    }
});
  • this is most complex code, probably

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

  • read tutorial about JTree

  • read tutorial about Concurency in Swing,

  • especially description about 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,

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

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();
}}; 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!