8

I have a table in a database named "Process"

This process table has 3 fields:

  • process_id
  • process_name
  • 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?


EDIT:

I want the graphical format something like this:

Tree hierarchy

Sk8erPeter
  • 6,899
  • 9
  • 48
  • 67
Amit
  • 33,847
  • 91
  • 226
  • 299
  • The obvious problem with this is that it is better to put something like this in a scroll pane, either horizontal or vertical. Otherwise the graph could potentially scale enormously in both directions and there is not a good way to handle that. Therefore, JTree is not a bad option. – Alexander Mills Sep 25 '13 at 02:56

4 Answers4

16

Swing has a built-in control for displaying data in a Tree format called JTree. It also provides a data model called DefaultTreeModel which you can use to store the data. This link gives a pretty good explanation of using a JTree with a data model.

[Edit]:

To answer your updated question, a graph like that could quite well be handled by JGraph which would also provide the data model which seems to work similarly to the Tree model swing provides.

FiroKun
  • 335
  • 2
  • 3
  • 14
Kentaree
  • 388
  • 1
  • 7
5

As others have suggested, using JTree is one possible solution. However, if you wish to have more control over the appearance of your tree I suggest checking out the JUNG graphing library. This can potentially give you great results but will require more work.

Adamski
  • 54,009
  • 15
  • 113
  • 152
3

For the hierarchy in graphical format, what about a JTree ? http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html

Pik'
  • 6,819
  • 1
  • 28
  • 24
2

Re Q1: You could find on internet resources several Java implementations of Tree structure, for example this generic tree.

Re Q2: One alternative is using JTree. Check the Java Tutorial trail on How to use Trees

JGraphT could be a good starting point to build something like your example. Check the demo at http://jgrapht.sourceforge.net/visualizations.html

JuanZe
  • 8,007
  • 44
  • 58