Metric Converter - PowerPoint PPT Presentation

About This Presentation
Title:

Metric Converter

Description:

Set which pane shows initially. tabs.setSelectedIndex(0); Containers. Class Signature line ... The valueChanged method handles the event ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 13
Provided by: Harv55
Learn more at: http://cs.sou.edu
Category:

less

Transcript and Presenter's Notes

Title: Metric Converter


1
Metric Converter
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
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
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
  • 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.getPat
    h()))
  • treeModel.nodeStructureChanged(parent)
  • tree.setSelectionPath( (newTreePath(node.getPath()
    )))
  • Display tree for (int r0 rlttree.getRowCount()-1
    r) tree.expandRow(r)
  • Collapse tree for (int rtree.getRowCount()-1rgt
    0r--) 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.getLastSelectedPathCo
    mponent() 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 nodes
    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 childX1 childXlt4 childX)
  • child new DefaultMutableTreeNode
  • (Node childName)
  • root.add(child)
  • for (int grandX1 grandXlt4 grandX)
  • grandChild new DefaultMutableTreeNode
  • (Node
    childName)
  • child.add(grandChild)

9
A Tabbed Panel
  • Instantiate a TabbedPane
  • 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
  • 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()"Ad
    d")
  • 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.
Write a Comment
User Comments (0)
About PowerShow.com