Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Dynamically updating a JTree  RSS feed

 
Paul Mrozik
Ranch Hand
Posts: 117
Chrome Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've spent all day on this and I simply can't figure out a clean way to do this.

Have a look at this diagram to get a better picture of what I want:




I have a Derby database where I store my data. Here's what happens when the program loads:

1. Connects to Derby DB
2. Populates an enum singleton peopleList with People
3. Creates a DefaultTreeModel based on the peopleList
4. Creates a JTree based on the DefaultTreeModel


Now here's where it gets really dirty:

When a user changes information about a Person and clicks Save, the following happens:

1. Derby DB is updated first via SQL
2. The person that was updated is again retrieved from the DB and a Person object is created
3. The "old" person is replaced with the "new" person on the peopleList and an updated flag is set to true in the peopleList
4. The updated flag is checked when the valueChanged(TreeSelectionEvent evt) method is called for the TreeSelectionListener
5. tree.getLastSelectedPathComponent() is called to retrieve the source node, and that same node is updated via the node.setUserObject(Object o) method.

I'm sure there must be a much smoother way to do this, but I don't know how. I wish I could just add a listener to the collection which could update Swing components. I have discovered Glazed Lists but before I go ahead and try to use them, perhaps there's another workaround?

I'd be grateful for your help as this has been a major bottleneck in my project.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Others may sneer at this, but here's what I did a long time ago when I was writing some code which managed large numbers of JTrees: I wrapped the user object in another object which contained a link to the tree node which contained that object. That way when I changed an object, I had a link to the tree node and I could cause the GUI for that node to be updated very simply.
 
Paul Mrozik
Ranch Hand
Posts: 117
Chrome Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Others may sneer at this, but here's what I did a long time ago when I was writing some code which managed large numbers of JTrees: I wrapped the user object in another object which contained a link to the tree node which contained that object. That way when I changed an object, I had a link to the tree node and I could cause the GUI for that node to be updated very simply.


Interesting and clever. I'll try it, thanks!
 
m Korbel
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
- be sure that all notifiers are fired, more see in Oracle tutorial How to use trees - Creating a Data Model

- especially there is missing the notifier for update, JTrees view isn't properly repainted with a new value
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!