如何在 Java 中显示树层次结构?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2101468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display tree hierarchy in Java?
提问by Yatendra Goel
I have a table in a database named "Process"
我在名为“Process”的数据库中有一个表
This process table has 3 fields:
这个进程表有 3 个字段:
- process_id
- process_name
- process_parent_id
- process_id
- 进程名称
- process_parent_id
Now I want to display this parent child hierarchy in Graphical format. So could you please suggest me the following:
现在我想以图形格式显示这个父子层次结构。那么你能不能给我建议以下内容:
Q1. Which data structure would be better to use so as to get data from the database and store in that data structure?
一季度。哪种数据结构最好使用以便从数据库中获取数据并存储在该数据结构中?
Q2. How to display that tree (process hierarchy) in graphical format?
Q2。如何以图形格式显示该树(流程层次结构)?
EDIT:
编辑:
I want the graphical format something like this:
我想要这样的图形格式:
回答by Kentaree
Swing has a built-in control for displaying data in a Tree format called JTree. It also provides a data model called DefaultTreeModelwhich you can use to store the data. This linkgives a pretty good explanation of using a JTreewith a data model.
Swing 有一个内置控件,用于以树格式显示数据,称为JTree. 它还提供了一个数据模型DefaultTreeModel,您可以使用它来存储数据。此链接很好地解释了如何将 aJTree与数据模型一起使用。
[Edit]:
[编辑]:
To answer your updated question, a graph like that could quite well be handled by JGraphwhich would also provide the data model which seems to work similarly to the Tree model swing provides.
为了回答您更新的问题,可以很好地处理这样的图,JGraph它也可以提供数据模型,该模型的工作方式似乎与摆动提供的 Tree 模型类似。
回答by Adamski
As others have suggested, using JTreeis one possible solution. However, if you wish to have more control over the appearance of your tree I suggest checking out the JUNGgraphing library. This can potentially give you great results but will require more work.
正如其他人所建议的那样,使用JTree是一种可能的解决方案。但是,如果您希望对树的外观有更多的控制,我建议您查看JUNG图形库。这可能会给你带来很好的结果,但需要更多的工作。
回答by Pikrass
For the hierarchy in graphical format, what about a JTree ? http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html
对于图形格式的层次结构,JTree 怎么样? http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html
回答by JuanZe
Re Q1: You could find on internet resources several Java implementations of Tree structure, for example this generic tree.
回复 Q1:您可以在 Internet 资源上找到树结构的几个 Java 实现,例如这个通用树。
Re Q2: One alternative is using JTree. Check the Java Tutorial trail on How to use Trees
回复 Q2:一种替代方法是使用 JTree。查看有关如何使用树的 Java 教程路径
JGraphTcould be a good starting point to build something like your example. Check the demo at http://jgrapht.sourceforge.net/visualizations.html
JGraphT可能是构建类似于您的示例的内容的良好起点。在http://jgrapht.sourceforge.net/visualizations.html检查演示


