0

As part of my homework, I've been asked to create a JTree Model with a fixed set of constants (Authors and Books) Presented below is only a small subset of constants to include. Is there a more elegant and maintainable solution to build the tree? I would have preferred to create a database but we have been advised that this is too complex at the moment. Any design advise or patterns would be much appreciated.

As a side note I couldn't find a homework tag.

public class TreeBuilder {
  public static ItemNode build(){
    ItemNode rootNode = new ItemNode("Library", ItemNode.NODE_ROOT);

    ItemNode authorOne = new ItemNode("Author One", ItemNode.NODE_AUTHOR); 
    ItemNode bookOne = new ItemNode("Book One", "isbn12345", ItemNode.NODE_BOOK);
    ItemNode bookTwo = new ItemNode("Book Two", "isbn54321", ItemNode.NODE_BOOK); 

    authorOne.addChild(bookOne);
    authorOne.addChild(bookTwo);

    ItemNode authorTwo = new ItemNode("Author Two", ItemNode.NODE_AUTHOR);
    ItemNode bookThree = new ItemNode("Book Three", "isbn98765", ItemNode.NODE_BOOK);

    authorTwo.addChild(bookThree); 

    rootNode.addChild(authorOne);
    rootNode.addChild(authorTwo); 

    return rootNode;
  }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319

1 Answers1

0

TreeDemo, discussed in How to Use Trees, is a book-related example. The createNodes() method creates the tree nodes. The first few examples progress in complexity, so you might start with the first.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045