java DirTreejava DirTree ~henrik/labbarShow details is checked.
Life and the root should have three children, Plants, Animals and Fungus, which in turn contains classes, orders, families and species.
Start with some minor modifications:
private void buildTree() and add code that does the following.
child in the same way as the root node but with the text Plants.
root as its parent. You can see how this is done some rows below in the code.
<Biosfär namn="Liv"> är allt som fortplantar sej <Rike namn="Växter"> kan inte förflytta sej </Rike> <Rike namn="Djur"> kan förflytta sej </Rike> <Rike namn="Svampar"> är varken djur eller växter </Rike> </Biosfär>Apart from the name of the node there is also a level (i.e. Rike) and details but, unfortunately, there is nowhere to store this information in the class
DefaultMutableTreeNode. You should therefore create a subclass Node which contains the String variables level and text. Now, all that is needed is a recursive method that should be called with:
root = readNode(); //Reads the XML-file and created the whole treeBefore the method is called the program should have "eaten" the first angle bracket from the file so that
readNode, after creating an empty node, can start reading the start tag and setting the level and attribute with node.setUserObject(...), "eat" the ending angle bracket, call readBetween(node) that reads the details and adds any possible child nodes.
When ending tag is reached it should be read and compared with the starting tag and eventual RuntimeException should be thrown, but normally it should return the newly created node. The method is described in lecture 7.
Just as the original program DirTree, your program should be able to take a file name as an argument:
java LifeTree life-file
and if no file name is given, the program should use the file Liv.xml.
Art: Mås gillar Vaxholmsbåtar. Alter the code so that showDetails(TreePath p) prints this text in its JOptionPane.
Extra assignment:
Alter the code so that it prints out the whole chain on the form but all that is Mås is Fåglar is Djur is Liv. Usefull method in TreePath is getLastPathComponent() and in DefaultMutableTreeNode, and thus in Node, the method getParent().
A tree full of life by (student) ........................................................................
appreciated (teacher) ............................................. on (date) ........................