1

Okay, I'm very new to Java so please bear with me.

I am using Netbeans 6.8 to write a small desktop application that includes a JTree component, and my requirement is that I be able to save and load the tree structure, but that the structure allows for three items of information per tree node - The text to be displayed as the node, a unique identifier, and a file name.

Of course I would also need to be able to get all three bits of information when a node is clicked.

I have successfully saved the and loaded the tree model using getModel() and XMLDecoder/encoder, but this of course only saves the default tree model.

I have followed several tutorials on creating a custom tree model but I have found them confusing, and I have been unable to transfer what they were telling me into my own project, since of course they usually create an entire example application in one go.

Assuming that a tree model like this is even possible please could someone explain how to go about creating such a model, and most importantly, how to place the model into a JTree that already exists in the application (and how I would go about retrieving the information when a node is clicked - I can currently find the selected node and retrieve its text)

If this is not possible Id gladly hear any alternative methods that result in the same functionality.

A solution has been reached so thanks to everyone for their efforts, however the correct answer must go to Andrew for posting it first!

For those interested: I used the Netbeans IDE to create a new Javabean object. Once I realised the difference between this and a normal object I managed to place a newly created Javabean object into a tree node. Just to make it all good, my original method of saving still works fine!

thanks for your efforts everyone.

MVK

MaxVK
  • 327
  • 4
  • 14

2 Answers2

4

The text to be displayed as the node, a unique identifier, and a file name.

Encapsualte them in a JavaBean Object and use an appropriate renderer.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    If that does not answer your question, please ask one. – Andrew Thompson Jun 29 '12 at 14:23
  • Thanks Andrew, but I am no nearer to understanding how to create a custom table model than I was before! I can encapsulate the required fields in an object without a problem, but I don't know how to get such an object into a tree model (and then into a tree), and I have no idea at all what a renderer is, appropriate or not! Assuming that I did get the object into a tree model, and into a tree, would I then still be able to save the tree structure to an XML file and load it again? – MaxVK Jun 29 '12 at 14:48
  • Presuming the object itself is a [JavaBean](http://docs.oracle.com/javase/tutorial/javabeans/index.html), yes. – Andrew Thompson Jun 29 '12 at 14:54
  • 4
    You can set an user object of DefaultMutableTreeNode which DefaultTreeModel uses. No need to create your own TreeModel. – Walter Laan Jun 29 '12 at 14:56
  • Thanks again Andrew - If I create an object: "public class NodeInfo...." is that a JavaBean? Would I then use DefaultMutableTreeNode to place it into my model or some other method? Also, am I missing a part of a conversation? I don't see a post by Soronthar? – MaxVK Jun 29 '12 at 15:10
  • @AndrewThompson oops, you saw my comment before I deleted it. I used the wrong link, sorry. – Soronthar Jun 29 '12 at 20:07
  • *"public class NodeInfo...." is that a JavaBean?* A bean requires public getter and setter methods, where the names map back to the attribute name. Hav you solved it. I note you 'accepted' my answer. – Andrew Thompson Jun 30 '12 at 00:09
  • Yes, Andrew thanks, all sorted now. I had to read through the `Javabean` spec a few times to get my head around the difference, and I employed `toString()` as well as custom setter and getter methods. Everything now works per requirement, many thanks. – MaxVK Jun 30 '12 at 17:14
1

By default, JTree uses a DefaultTreeModel which in turn uses a DefaultMutableTreeNode to represent each node. You can assign any object to a node, and it will be rendered by calling it's toString() method.

So, the simplest solution is to encapsulate all the data you need in an class and implement the toString() method to return whatever you want to be displayed. As an added bonus, if you implement your class following the JavaBeans spec, you get XML serialization for free.

You really should take a look at the Swing tutorial, specifically How to use Trees. Models, renderers and editosr will be more clear after you read how to handle trees, list and tables.

Soronthar
  • 1,601
  • 10
  • 10
  • Sorry Soronthar, I couldn't see your post earlier. Thanks for the reply. Iv read the How to use Trees tutorial, and I tried to follow one of the examples there, but could not reconcile what they explain with what I am trying to do within the netbeans environment. I have also started to read through the Javabeans spec (When Andrew supplied the link), but I am yet to work out how to create a JavaBean that contains three strings and can be used as a node in a jtree. – MaxVK Jun 29 '12 at 15:23
  • *"create a JavaBean that contains three strings"* BTW - what is *"text to be displayed as the node"*? Is it the '3rd string'? – Andrew Thompson Jun 29 '12 at 15:41
  • The 'text to be displayed' is what is seen on the tree in the application and is the first string, a unique ID is the second string, and a filename is the third string. When the node is clicked I need to return all three so that I can correctly load a file and display a message. – MaxVK Jun 29 '12 at 15:44
  • 2
    +1 Here's an [example](http://stackoverflow.com/a/11113648/230513) of Andrew, Walter and Soronthar's suggestion to leverage the default model's `userObject`. – trashgod Jun 29 '12 at 17:46