3

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 :

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

Bujangan Muda
  • 88
  • 1
  • 7

3 Answers3

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:

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);
        }
    }
});
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
2
  • 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

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • @BujanganMuda: I think you'll have to refresh the model from the file, as shown [here](http://stackoverflow.com/a/11875615/230513). – trashgod Aug 09 '12 at 01:39
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();
}}; 
Muhammad Cahya
  • 288
  • 4
  • 13
  • Good idea, but I think he'll have to refresh the model from the file, as shown [here](http://stackoverflow.com/a/11875615/230513). BujanganMuda: See also this [Q&A](http://stackoverflow.com/q/11790390/230513). – trashgod Aug 09 '12 at 01:41