I do not understand what the problem is.
DefaultMutableTreeNode will use the toString method for the user object because it makes sense. JTree needs strings to draw objects, so the request to your object will look fine.
If you really need to avoid calling toString on your object, you'll need a way to provide a string representing it anyway, but you will have to write your own MutableTreeNode :
class MyTreeNode implements MutableTreeNode { UserObject yourObject; MyTreeNode(UserObject yourObject) { this.yourObject = yourObject; }
But I really see no reason to do this. Alternatively, you can try extending DefaultMutableTreeNode by overriding the toString method, but you will need an additional reference to your object, or you will need some downgrades.
If you really need a different visualization than a string, you will have to write your own rendering that implements TableCellRenderer .
source share