• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt about user object returned on treeNodesChanged method / TreeModelListener - Beginner

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there...

I have JTree made of DefaultMutableTreeNode nodes. These nodes are created with a very simple user object inside.

example:

category = new DefaultMutableTreeNode(new Directory(key,hm.get(key)));

Every time I trigger the treeNodesChanged event and try to access the Directory object inside treeNodesChanged it is apparently "replaced" by a String object.

example:

public void treeNodesChanged(TreeModelEvent e) {
DefaultMutableTreeNode node;
node = (DefaultMutableTreeNode)(e.getTreePath().getLastPathComponent());
System.out.println(node.getUserObject().getClass()); -> AT THIS POINT I WAS EXPECTING TO PRINT THE DIRECTORY CLASS BUT WHAT IS RETURNED IS "class java.lang.String"
.........
....
......
..
.
}

Can someone help me understand what am I doing wrong?


Thanks in advance
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strange.
Why do you have a tree model listener? Are you modifying the nodes at runtime, after the tree is displayed? Usually, one uses a tree selection listener to figure out user selection.
When the tree nodes changed is fired, which node is selected on the UI? Is it the root? Is the root also constructed using the Directory object?

I created a simple tree like this
Then I added a selection listener to the tree and I print out the node's user object. If I select one of the drives, I correctly get java.io.File on the console. But if I select the root, I get java.lang.String

Is something similar happening in your case?
If not, you will need to post sscce code for us to help you. Please do visit the link to find out what sscce really means.
 
Daniel Rodrigues
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Thanks for your reply...

Yes, I am changing the nodes at running time (set the JTree as editable)
I found an user with exactly the same issue I have in that link "http://72.5.124.102/thread.jspa?messageID=2232016" with the subject:

Swing [Archive] - getUserObject() should really return my object, not a String object.

Look what I found.... (I believe that the previous person could explain better the issue than I)

"
When you edit a treenode, the tree sets the user object ot be a string, because it doesn't know any better...

The TreeCellRenderer (I believe) is responsible for the call to the TreeModel.valueForPathChanged(TreePath p,Object value) and it passes a String which it obtains from its editor component (a JTextField).

DefaultTreeModel then calls, MutableTreeNode.setUserObject(), hence the resulting String.

Sooo....
You have two choices:
1) rewrite TreeCellEditor to parse the String into your Thing
2) rewrite the DefaultTreeModel to convert the String to your Thing
"


Let me know if you know a easier way to fix that...

Thanks
 
Daniel Rodrigues
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Gentlemen,

Just to close this thread I will add the code and some of the references I used to persist the original userObject after changes in an editable tree node.
I understood that whenever you use custom objects instead of "Strings" you will need to do that.

A very good explanation about how to work with JTree can be found in the book

"Core JFC, 2nd Edition By Kim Topley" (I could find the chapter about JTree in the web...)

"To ensure that the value stored in a node after the edit has completed is an object of the correct type, it will be necessary to supply our own tree model in which the valueForPathChanged method is overridden."

In see below my TreeModel



When treeNodesChanged is called I did:



My user object ...



This worked for me, however, I am not a Java professional and probably there are better ways to do it but I am happy with the results.
Maybe this can help someone else (I took alot of time to find out that ))))

 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Daniel, please UseCodeTags.
 
Daniel Rodrigues
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
done... Thanks for the advice
 
reply
    Bookmark Topic Watch Topic
  • New Topic