I have some questions regarding a treemodel in Java.
For the last 13 weeks in class we've been developing a contact manager. This contact manager has various components: a contact can either be a business contact or personal contact, and each has their own set of events. They also have addresses, social networks, and phone numbers.
Last assignment we had to read information from an outside txt file and implement it in our project. That went fine. Now he wants us to build a treeModel out of the information from said text, but I'm having a really hard time understanding how this tree model works.
For example, root, parent, children, nodes are all words I'm trying to define but having a really hard time. In this case would my root be my contact manager, my parent by a contact and children be like address and stuff? If someone could help me understand it it would really help.
Here's a excerpt from the assignment if it helps you understand my question more:
Create class ContactManagerFrame that extends JFrame. Its constructor will take as argument a ContactManager object (retrieved from the serialized file saved on disk).
Create and layout the required graphical components. (Hint: use a JPanel to group the components of the filtering feature, add the JTree to a JScrollPane, use BorderLayout when adding the components)
Create method createNodes() that takes as arguments a DefaultMutableTreeNode corresponding to the root of the tree (i.e., “Contacts” node) and a String corresponding to the filter specified by the user. This method should create nodes for the contacts and the objects referenced by each contact and organize them in a tree structure, as shown in Figure 1. You should avoid the creation of empty parent nodes (e.g., if a contact does not have any addresses, you should not add a parent node “Addresses” for this particular contact). If the specified filter is an empty string, createNodes() will include all contacts stored in the ContactManager object. In any other case, createNodes() will include the contacts returned by method searchContacts() in ContactManager.
Create a DefaultTreeModel object from the root node. Use the DefaultTreeModel to initialize the JTree component.'
I'm not asking any of you to solve it for me or anything. I just really want to understand it. Thank you very much!
More sophisticated Swing components such as JTree, JTable, JList or JComboBox work with the concept of model. It means: the subjacent data that is being displayed by the component. They are designed in this way to have separate the data itself from their "visual" representation (a.k.a. the view) and allow the developer "forgot" (more or less) about data representation. So as explained in the tutorials, if you need to add a new data object to be displayed on one of these components you only have to add it to the model and the view will be automatically updated.
Having said this you'll see these components have a constructor that takes a model as argument:
- JTree(TreeModel newModel)
- JTable(TableModel dm)
- JComboBox(ComboBoxModel aModel)
- JList(ListModel dataModel)
These models are defined by interfaces which establish the basic contract that any concrete implementation must meet.
Particulalry in JTree
's case we have the TreeModel interface and a default implementation: DefaultTreeModel. Additionaly any TreeModel
has to work with node objects which have to implement the TreeNode interface.
So, to work with JTree
you'll need a TreeModel
and a bunch of TreeNodes
related through a parent-child relationship. For instance something like this:
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Contacts"); // root node
DefaultMutableTreeNode contact1 = new DefaultMutableTreeNode("Contact # 1"); // level 1 node
DefaultMutableTreeNode nickName1 = new DefaultMutableTreeNode("drocktapiff"); // level 2 (leaf) node
contact1.add(nickName1);
DefaultMutableTreeNode contact2 = new DefaultMutableTreeNode("Contact # 2");
DefaultMutableTreeNode nickName2 = new DefaultMutableTreeNode("dic19");
contact2.add(nickName2);
root.add(contact1);
root.add(contact2);
DefaultTreeModel model = new DefaultTreeModel(root);
JTree tree = new JTree(model);
Picture
来源:https://stackoverflow.com/questions/20250752/treemodel-creation