posted 14 years ago
@Paul Clapham: Hi, I followed your suggestion, and the tree is updated ok, but I am not sure if my implementation is exactly how you would expect it or if it is messy. I will describe what I did and I hope that you or at least someone can point out where my design should change (if it is indeed bad) so that I can understand how to work with Swing better.
The suggestion was to have pointers to the tree nodes inside the objects, so that when an object's value is changed, I can use the reference to update the tree.
Since the object containing the data of interest, should not be modified (not created by me and not allowed to change them), I created a hashmap, that corelates an object to a particular DefaultMutableTreeNode. Whenever an object was created, a new entry in the hasmap was added and a node in the tree also. I updated the tree using treeModel's nodeStructure changed. If an object was changed, I used the hashmap to get the treenode and update it. I am not sure if this is the way to go or I should implement a custom treemodel since these concepts are not clear in my mind yet. Another problem I am concerned with, is that the code is a little messy. I have a class named model, and I endup updating the treenodes inside the model.
I.e. The user presses a button. The view class does some processing in the actionPerformed and the processing function of my model is called. I have ended up doing the update of the tree inside my model. Is this bad design? I should return the result back to view class, to update the tree?
Any review comment is appreciated, since swing is confusing to me. Thank you.