Search code examples
javauser-interfacecanvasswtdraw2d

Displaying Hierarchical Tree Structure on Canvas Java SWT Draw2d


I have a tree data structure which stores information. How do I figure out the correct placement of each node i.e. x,y locations of the tree on the canvas, so that nothing overlaps and all sibling nodes are on the same level? My attempts achieve neither. Thank you very much. I had something like this in mind, to be displayed on the canvas.

      A
     / \
    B   C
  / | \  \
 D  E  F  G
           \
           H

Solution

  • Rather than reinventing the wheel, you could simply use a library that already implements several layout algorithms.

    Zest seems the best choice here.

    Here you can find a brilliant article about it and especially this section about Layout Managers should help you (TreeLayoutAlgorithm).