简体   繁体   English

每个JTree节点存储和使用多个值

[英]Storing and using multiple values per JTree node

Okay, I'm very new to Java so please bear with me. 好的,我是Java的新手,所以请多多包涵。

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. 我正在使用Netbeans 6.8编写一个包含JTree组件的小型桌面应用程序,我的要求是我能够保存和加载树结构,但是该结构允许每个树节点包含三项信息-文本显示为节点,唯一标识符和文件名。

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. 我已经成功保存了,并使用getModel()XMLDecoder / encoder加载了树模型,但这当然只保存了默认树模型。

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) 假设甚至可以使用这样的树模型,请有人解释如何创建这样的模型,最重要的是,如何将模型放入应用程序中已经存在的JTree中(以及我将如何检索该模型)。单击节点时的信息-我目前可以找到选定的节点并检索其文本)

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! 已经找到了解决方案,因此感谢大家的努力,但是正确的答案必须交给Andrew首先发布!

For those interested: I used the Netbeans IDE to create a new Javabean object. 对于那些感兴趣的人:我使用Netbeans IDE创建了一个新的Javabean对象。 Once I realised the difference between this and a normal object I managed to place a newly created Javabean object into a tree node. 一旦意识到了该对象与普通对象之间的区别,便设法将新创建的Javabean对象放入树节点中。 Just to make it all good, my original method of saving still works fine! 只是为了使一切顺利,我原来的保存方法仍然可以正常工作!

thanks for your efforts everyone. 感谢大家的努力。

MVK MVK

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. 将它们封装在JavaBean Object并使用适当的渲染器。

By default, JTree uses a DefaultTreeModel which in turn uses a DefaultMutableTreeNode to represent each node. 默认情况下,JTree使用DefaultTreeModel,后者依次使用DefaultMutableTreeNode表示每个节点。 You can assign any object to a node, and it will be rendered by calling it's toString() method. 您可以将任何对象分配给节点,并且将通过调用toString()方法来呈现该对象。

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. 因此,最简单的解决方案是将所需的所有数据封装在一个类中,并实现toString()方法以返回要显示的内容。 As an added bonus, if you implement your class following the JavaBeans spec, you get XML serialization for free. 另外,如果按照JavaBeans规范实现类,则可以免费获得XML序列化。

You really should take a look at the Swing tutorial, specifically How to use Trees . 您确实应该看一下Swing教程,特别是How to use Trees Models, renderers and editosr will be more clear after you read how to handle trees, list and tables. 在阅读了如何处理树,列表和表格后,模型,渲染器和editosr会更加清晰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM