0

Here is my application.

enter image description here

What I want to do is make it so that when the user's details are updated, so does it's node in the JTree. Is this possible; even though the JTree and it's rendered cells have already been created?

I need an example of updating a rendered cell even after it has already been created.

Here is my View code:

void createTree() {

    rootNode = new DefaultMutableTreeNode("users");

    for (User user : listOfUsers) {

        String id = user.getId();
        File file = new File(id);
        DefaultMutableTreeNode node = new DefaultMutableTreeNode(file);
        rootNode.add(node);
    }

    treeModel = new DefaultTreeModel(rootNode);
    tree = new JTree(rootNode);
}

void addTreeModelListener(TreeModelListener a) {

    treeModel.addTreeModelListener(a);
}

void repaintTree() {

    treeModel.reload();
    tree.revalidate();
    tree.repaint();
}

And here is my Controller code:

view.addTreeModelListener(new MyTreeModelListener());

class MyTreeModelListener implements TreeModelListener {
    public void treeNodesChanged(TreeModelEvent e) {

        DefaultMutableTreeNode node;
        node = (DefaultMutableTreeNode)(e.getTreePath().getLastPathComponent());

        int index = e.getChildIndices()[0];
        node = (DefaultMutableTreeNode)(node.getChildAt(index));

        System.out.println("The user has finished editing the node.");
        System.out.println("New value: " + node.getUserObject());       
    }

    public void treeNodesInserted(TreeModelEvent e) {}
    public void treeNodesRemoved(TreeModelEvent e) {}
    public void treeStructureChanged(TreeModelEvent e) {}
}

The idea is after the changes to the objects have been made, I call the repaintTree() method. After looking over the code now, I think the problem lies in the fact that I'm changing the object details and not the node details, but that's just a hunch.

Please reply soon, many thanks.

Kurtiss
  • 495
  • 2
  • 7
  • 22

1 Answers1

2

Swing uses MVC architecture.

So data contained in the tree and the logic for rendering are completely decoupled and independent.

You can simply change contents of the tree without worrying using this Oracle Tutorial. Just don't forget to call revalidate() and repaint() method after updating.

Good luck.

Tanmay Patil
  • 6,882
  • 2
  • 25
  • 45
  • Thanks for the prompt reply and examples. I've been referring to them and I'm still having problems getting it to work at the moment. What it does though is when I save, the JTree unfocuses itself; maybe because the JTree is revalidating, except it's not updating the rendered cells for some reason. – Kurtiss Apr 04 '14 at 13:22
  • Did you call both revalidate and repaint? – Tanmay Patil Apr 04 '14 at 13:38
  • yeah I did, I made a tree model and tried reload() as well. – Kurtiss Apr 04 '14 at 13:39
  • After making changes to the TreeModel, you have to call it's reload method. – Tanmay Patil Apr 04 '14 at 13:51
  • I found out what the problem was; even though I was updating the objects, I was also required to update the list of users as well. Does that mean that when you create a list of objects, you're actually copying the objects over to create the list , as opposed to transferring the objects themselves? – Kurtiss Apr 05 '14 at 15:39
  • It depends upon the underlying implementation. Which list are you talking about? `LinkedList`/`ArrayList`/`Vector` don't copy the objects. – Tanmay Patil Apr 05 '14 at 19:24
  • ArrayList listOfUsers; – Kurtiss Apr 05 '14 at 20:22
  • You are not adding user to the list, just the file object from it. So I don't think user should be directly updated if you are changing it's reference. See http://stackoverflow.com/questions/40480/is-java-pass-by-reference – Tanmay Patil Apr 05 '14 at 20:29
  • Many thanks for your help. The list should of been defined within the constructor in order for it to behave as intended. Again, many thanks. – Kurtiss Apr 07 '14 at 11:19