How to draw a tree in java language

Go To


I am trying to parse an XML file in java, after I have to represent it as a tree using Jframe like thisenter image description here

2012-04-04 17:39
by Wassim Sboui
Your question I am afraid is very ambiguous at the moment. Please add additional information so that the community may better assist you with an answer - JDD 2012-04-04 17:41
Assuming you want to replicate the figure, why Java? Any specific reason? Else use Dia - NoName 2012-04-04 17:41
I am trying to parse an XML file in java, after I have to represent it as a tree using Jfram - Wassim Sboui 2012-04-04 17:49


Trees are generally one of the easier linked constructs to lay out like this, because paths generally don't "merge" or "cross".

You can approach it in a roughly tabular way by traversing the tree "left to right": start at the root, and draw its representation at the top left of the area. Then, traverse its "left" branch one level at a time, drawing those nodes' representations on successively lower "rows", in the same "column" as the root. Then, as you move to the "right"-side nodes, draw that node in the next "column" available to the right on the same level. This will produce a ramp-shaped graph of the tree's structure.

You can add some pre-analysis of the number of levels and nodes at each level, which will allow you to "center" the tree into a rough pyramid shape by knowing the maximum number of levels the graph will require and the number of nodes at each level of that graph. But, that requires traversing the entire graph before you start drawing anything.

As for "arranging" a tree's nodes so they fit in the smallest area without arrows crossing or overlapping, that's a problem with a scope far exceeding the average SO answer.

2012-04-04 17:53
by KeithS


There are lots of good libraries for visualizing graphs.

Here's a pretty extensive list of options:

2012-04-04 17:42
by ulmangt


Prefuse could probably create something aesthetically similar. Of course, you could go the primitive route and do the graphics manually. For an XML to graph transform, the TreeMLReader API may be of some use, however you might have to convert the XML to the TreeML format with XSLT first.

2012-04-04 17:48
by blackcompe


Us the standard forms to print the image

2012-04-04 17:52
by RyanS