JavaAssignment6

Embed Size (px)

Citation preview

  1. 1. Collections in JAVACollections in JAVACollege of Dupage CIS2572 Spring 2011 Instructor M MorovatiCollege of Dupage CIS2572 Spring 2011 Instructor M Morovati Assignment #6Assignment #6 ByBy StudentStudent Art.SArt.S
  2. 2. CIS2572-001 Assignment #6 Create a Java application for Family Members that represents your family tree. Use a JTreeJTree to represent the hierarchy Create a class called FamilyMember which has the following attributes: - NameName - AgeAge - Spouse NameSpouse Name - List of childrenList of children (children are instances of FamilyMember) - NationalityNationality - State of residenceState of residence Create all the applicable get/setget/set methods as well as toStringtoString method which prints out the -name-name, -age-age and -spouses-spouses namename - Create a printInfo()printInfo() methodmethod which formats the information about a family member to be used in a text areatext area - Display name, age, spouses name, number of children, names of children, nationality, and state of residence - Create a method which instantiatesmethod which instantiates all the family members in your family. - Start with your parent or maternal grandparentgrandparent all the way to you and your children. - The tree should matche your family hierarchy - Use JTreeJTree to represent your family members in a hierarchicalhierarchical view - -Create a TreeSelectionListenerTreeSelectionListener that displays the printInfoprintInfo on the selectedFamilyMember in the -JTreeJTree within a JTextAreaJTextArea or JEditorPaneJEditorPane on the same window as the JTreeJTree resides. - Set JTree editing to false (prevent changes to family member name and the structure of tree once it was build) -Implement a TreeCellRenderer to show an appropriate icon for the non-leaf and leaf nodes in the JTree -(not the default icons) - Implement ToolTipsToolTips to show the same info when you hover the mouse over a FamilyMemberFamilyMember. - Also show the same info when you right -clickright -click on the FamilyMember. - Implement Drag and DropDrag and Drop to support the draging a child from one branch to another - Implement Edit, Add, DeleteEdit, Add, Delete, Save to be able to add new member, delete a member, or modify a member information. - Also to be able to save the FamilyTreeFamilyTree to a file using the FileChooserFileChooser.
  3. 3. ORACLE UML Engineering Blueprint DesignORACLE UML Engineering Blueprint Design College of DupageCollege of Dupage Assigment #6Assigment #6 ByBy Art.SArt.S CIS2572 Spring 2011CIS2572 Spring 2011
  4. 4. Family Tree (greatgrandparents)Family Tree (greatgrandparents)
  5. 5. Family Tree (immediate)Family Tree (immediate)
  6. 6. package client; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Toolkit; import javax.swing.JFrame; public class familytrees { public static void main(String[] args) { FamilyThreeClass applet = new FamilyThreeClass(); JFrame frame = new JFrame(); //EXIT_ON_CLOSE == 3 frame.setDefaultCloseOperation(3); frame.setTitle("FamilyTreeClass"); frame.getContentPane().add(applet, BorderLayout.CENTER); applet.init(); applet.start(); frame.setSize(650, 320); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }
  7. 7. jbtAdd.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { DefaultMutableTreeNode parent = (DefaultMutableTreeNode)jTree1.getLastSelectedPathComponent(); if (parent == null) { JOptionPane.showMessageDialog(null, "No node in the left tree is selected"); return; } // Enter a new node String nodeName = JOptionPane.showInputDialog(null, "Enter a child node for " + parent, "Add a Child", JOptionPane.QUESTION_MESSAGE); // Insert the new node as a child of treeNode parent.add(new DefaultMutableTreeNode(nodeName)); // Reload the model since a new tree node is added ((DefaultTreeModel)(jTree1.getModel())).reload(); try { // Store Record To Disk: java.io.PrintWriter output = new java.io.PrintWriter("c:" + RECORD); // new java.io.PrintWriter("c:" + FileName); output.print(RECORD + ":t" + nodeName + "n"); output.close(); } // try catch (Exception exit) { System.out.println("Found Errors Try Again..."); } // catch } });
  8. 8. jchkEditable.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jTree1.setEditable(jchkEditable.isSelected()); DefaultMutableTreeNode parent = (DefaultMutableTreeNode)jTree1.getLastSelectedPathComponent(); if (parent == null) { JOptionPane.showMessageDialog(null, "No node in the left tree is selected"); return; } // Enter a new node String nodeName = JOptionPane.showInputDialog(null, "Enter a child node for " + parent, "Add a Child", JOptionPane.QUESTION_MESSAGE); // Insert the new node as a child of treeNode parent.add(new DefaultMutableTreeNode(nodeName)); // Reload the model since a new tree node is added ((DefaultTreeModel)(jTree1.getModel())).reload(); try { //FileWriter modwriter = new FileWriter("c:" + RECORD); FileWriter modwriter = new FileWriter("c:" + RECORD); modwriter.write(RECORD + ":" + nodeName); modwriter.write("Total" + "n"); modwriter.close(); } catch (IOException ex) { ex.printStackTrace(); } } });
  9. 9. import javax.swing.tree.TreePath;import javax.swing.tree.TreePath;
  10. 10. import javax.swing.tree.TreePath;import javax.swing.tree.TreePath;
  11. 11. import javax.swing.tree.TreePath;import javax.swing.tree.TreePath;
  12. 12. import javax.swing.tree.TreePath;import javax.swing.tree.TreePath;
  13. 13. ConclusionConclusion EndEnd CIS2572:Assignment 6CIS2572:Assignment 6
  14. 14. END
  15. 15. ConclusionConclusion EndEnd CIS2572:Assignment 4/5CIS2572:Assignment 4/5