JTree befüllen (nested set)

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • JTree befüllen (nested set)

    Hallo,

    ich möchte gerne einen JTree populieren, aber irgendwie komme ich nicht weiter.


    Ich habe einen Vector mit den Objekten, die im Jtree dargestellt werden sollen. Die Objekte im Vector sind schon nach hierarchie-ebene sortiert. (Ich bin nach dieser Beschreibung vorgegangen). Der Inhalt des Vectors sieht dann so aus:

    [Blockierte Grafik: http://www.klempert.de/nested_sets/tab2.png]

    Quellcode

    1. Säugetiere
    2. * Primaten
    3. o Halbaffen
    4. o Affen
    5. * Nagetiere



    Jetzt weiß ich leider nicht, wie ich diese hierarchie am besten in den JTree bekomme.
    Dafür gibt es doch bestimmt einen (rekursiven) Algorithmus, oder?
    Könnt ihr mir ein paar Tipps oder Links zu Referenzimplementierungen geben? Würde mich sehr darüber freuen.
  • Habe inzwischen etwas gefunden, das funktioniert. Vieleicht interessiert die Lösung ja auch den ein oder anderen :)
    An die Funktion wird übergeben:
    ein Vector, in dem sich die Objekte befinden, die den Baum darstellen sollen
    Ein File objekt (custom Klasse), welches das Vaterelement darstellt. (Beim ersten Aufruf also das root-element des Baumes).
    Ein Int-Wert start, der angibt, ab welchem Punkt der Vector durchlaufen werden soll.
    Näheres dazu steht auch nochmal im Kommentar zur Funktion.

    Ich hoffe, man kann es so halbwegs verstehen :)
    Falls jemandem Unstimmigkeiten auffallen sollten (etwa im comment), dann wäre ich über einen Hinweis dankbar.

    Quellcode

    1. // files --> Vector holding File objects
    2. DefaultMutableTreeNode root = getNode(files, files.get(0), 1);
    3. JTree tree = new JTree(root);
    4. /**
    5. * This function will accept a Vector from which the tree will be build, a
    6. * parent object of type File (package main.File) and an integer start. The
    7. * function will search through the Vector, starting at position "start",
    8. * looking for File objects that have a higher level than parent. This
    9. * means, that they are children of parent. At this point, recursion appears
    10. * and the function will be called again for each child, this time using the
    11. * child as parent of a new subtree, which will hold all the child's
    12. * children. The search stops as soon as the next File object has a
    13. * level-value lower than the parent. According to the structure of the
    14. * Vector, the current branch ends here. In a "recursion-call", all the File
    15. * objects will be traversed again, even those that have been added to the
    16. * tree BUT have a lower number than the current parent-node. This would
    17. * cause an unwanted return of the function. To counter this, the part of
    18. * the Vector that is being searched needs to start at the element that
    19. * comes right after the parent element.
    20. *
    21. * Sample Vector structure:
    22. * File a - level 0
    23. * File b - level 1
    24. * File c - level 2
    25. * File d - level 2
    26. * File e - level 1
    27. * File f - level 2
    28. *
    29. * According tree structure:
    30. * a
    31. * . b
    32. * . . c
    33. * . . d
    34. * . e
    35. * . . f
    36. *
    37. *
    38. * @param v
    39. * - Vector containing all File objects that are to be inserted
    40. * into the tree.
    41. * @param parent
    42. * - parent File.
    43. * @param start
    44. * - indicates the position from where to go through the vector.
    45. * @return DefaultMutableTreeNode that already contains all children.
    46. */
    47. public DefaultMutableTreeNode getNode(Vector<MyFile> v, MyFile parent, int start) {
    48. DefaultMutableTreeNode node = new DefaultMutableTreeNode(parent);
    49. if (!parent.isDirectory()) {
    50. node.setAllowsChildren(false);
    51. }
    52. // loop through the vector
    53. for (int i = start; i < v.size(); i++) {
    54. // only consider entries that have a level 1 point higher than the parent (parent level + 1)
    55. if (v.get(i).getLevel() == parent.getLevel() + 1) {
    56. // recursion - get subtree of the new child and add it to the parent
    57. node.add(getNode(v, v.get(i), i + 1));
    58. } else {
    59. // stop looping once an element is encountered that has a lower
    60. // level than the parent
    61. if (v.get(i).getLevel() <= parent.getLevel()) {
    62. break;
    63. }
    64. }
    65. }
    66. return node;
    67. }
    Alles anzeigen

    Dieser Beitrag wurde bereits 3 mal editiert, zuletzt von crest ()