Presentation is loading. Please wait.

Presentation is loading. Please wait.

Metric Converter What layout do we use? What is the class hierarchy?

Similar presentations


Presentation on theme: "Metric Converter What layout do we use? What is the class hierarchy?"— Presentation transcript:

1 Metric Converter What layout do we use? What is the class hierarchy?
The code for this application is on our class web-site What layout do we use? What is the class hierarchy? What listeners do we use?

2 On-line Ordering Applet
The code for this application is on our class web-site What layout do we use? What is the class hierarchy? What listeners do we use?

3 Simple Text Editor Allows simple text editing What layout do we use?
The code for this application is on our class web-site Allows simple text editing What layout do we use? What is the class hierarchy? What listeners do we use?

4 Trees Graph: Collection of nodes and edges
Nodes are the objects that hold data Edges are links between nodes Tree: A graph without cycles Root: A distinguished node at the top of the tree Purpose Trees and graphs are probably the most used data structures in Computer Science They are useful to represent Directory structures Discussion forums Simulation of weather, climate change, global positioning Relational databases

5 Lab Project The main frame has a border layout
Create a GUI that displays and modify a tree The main frame has a border layout The North section has a JTabbedPane The Center section has a JScrollPane showing a JTree The South section has a label that displays user help messages

6 Sample JTree Methods More information is in the lab instructions
JTree related classes JTree (Java tree class) DefaultTreeModel (Object that controls how tree displays) DefaultMutableTreeNode (nodes to hold data in the tree) Instantiate A Node for the tree: root = new DefaultMutableTreeNode(“name”); Tree Model: treeModel = new DefaultTreeModel(root); A JTree: tree = Jtree(treeModel); Add a node to the root, make it display, and highlight the new node root.add(new DefaultMutableTreeNode("child node"); tree.scrollPathToVisible(new TreePath(child.getPath())); treeModel.nodeStructureChanged(parent); tree.setSelectionPath( (newTreePath(node.getPath()))); Display tree: for (int r=0; r<tree.getRowCount()-1; r++) tree.expandRow(r); Collapse tree: for (int r=tree.getRowCount()-1;r>=0;r--) tree.collapseRow(r); More information is in the lab instructions

7 Additional JTree methods
Path TreePath(node.getPath()); gets a path of nodes from root to node. Tree.scrollPathToVisible(path); makes a path visible on the display. treeModel.nodeStructureChanged(node); forces a node to be displayed. treeModel.removeNodeFromParent(node); removes a node from the tree. Node.getParent() gets the parent node. (DefaultMutableTreeNode)tree.getLastSelectedPathComponent() can be used to get the node which is selected Tree.setSelectionPath(new TreePath(node.getPath()) sets the selected node. Node.setUserObject(“new name”) changes the node’s text. treeModel.nodeChanged(node); to force a modified node to display Tree.expandPath(path) and Tree.collapse(path)

8 Sample JTree Code Instantiate and Attach a listener
DefaultMutableTreeNode root = new DefaultMutableTreeNode(“Root”); DefaultTreeModel treeModel = new DefaultTreeModel(root); JTree tree = new JTree(treeModel); tree.addTreeSelectionListener(this); Initially fill tree with nodes int childName = 0; For (int childX=1; childX<=4; childX++) { child = new DefaultMutableTreeNode (“Node “ + childName++); root.add(child); for (int grandX=1; grandX<=4; grandX++) { grandChild = new DefaultMutableTreeNode (“Node “+childName++); child.add(grandChild); } }

9 A Tabbed Panel Instantiate a TabbedPane Add a panel to the tabbed pane
JTabbedPane tabs = new JTabbedPane(); Add a panel to the tabbed pane JPanel treeChanges = new TreeChangePanel(); tabs.addTab(“Modify”, treeChanges); Set which pane shows initially tabs.setSelectedIndex(0);

10 Containers Class Signature line
Public class TreeChangePanel implements ActionListener Access to the JTree and other instance variables Alternative1: Pass references to the constructor Alternative 2: Nest Panel classes within the main class Set Layout in the TreeChanges class setLayout(new BoxLayout(this, BoxLayout,X_AXIS) ); Create and add the buttons JButton addButton = new JButton("Add"); add(addButton); Attach the listener addActionListener(this);

11 The ActionListener Signature line Determine which button was clicked
public void actionPerformed(ActionEvent ae) Determine which button was clicked Alternative 1: if (ae.getSource()==addButton) Alternative 2: JButton button = (JButton)ae.getSource(); if (button.getText()=="Add") Event specific logic (for the 'add' option) Set error label if a tree node is not selected Create a new node Add the new node to the node that is selected Set the path to the new node visible Indicate that the tree structure changed Select the node just added

12 TreeSelectionListener
This listener responds to changes in the JTree structure The valueChanged method handles the event It's purpose is to set the label field to the node that the user selects The initial version in the lab instructions can trigger a NullPointeException if there is no node selected How do we get around this? If there is no last selected path component, don't try to call the toString() method.


Download ppt "Metric Converter What layout do we use? What is the class hierarchy?"

Similar presentations


Ads by Google