Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Writting Custom Tree Model  RSS feed

 
Akhil Jain
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I need litle help here to get started in right direction. (sort of lost somewhere.).

I am trying to display an object in JTree component with all its premitive data as well as any other user-defined object it may contain.
Object which i am getting doesn't have to do anything with specific tree interfaces, they are simple POJO's which can contain number of user-defined objects in them. (sort of debugging view of an object). I can get all the information about object using my reflection utility. which can provide me every information in string form.

i just want to create a tree model for that object.
this is what i have done using HashTable with JTree.



.....code to access this model and create JTree is as follows




Problem with this approach. JTree constructor which takes the hashtable can not show root node. and only 1-level (up to one level expanding of node) is supported.

can some one help me out here.

regards
Akhil
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JTree depends on its TreeModel. To make your own TreeModel you would have your custom
model class implement the TreeModel interface. You can look at the source code of
DefaultTreeModel to get some ideas about implementing the methods defined in the TreeModel
interface.
Another option is to use a DefaultTreeModel in which these methods are already implemented
for you. You would need to make up a class that implements the TreeNode interface to be
used by the DefaultTreeModel. For ideas about implementing the methods defined in this
interface you can look at the source code for the DefaultMutableTreeNode class.
Another option is to use DefaultMutableTreeNode for a DefaultTreeModel. The userObject is
the user-defined object that is used in the DefaultMutableTreeNode. This way most of the
work of dealing with model, nodes, and events is taken care of for you.
You might put your custom object together into a JTree form along these lines:
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!