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?
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?
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?
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
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
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.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
Additional JTree methods
1. Path TreePath(node.getPath()); gets a path of nodes from root to node.
2. Tree.scrollPathToVisible(path); makes a path visible on the display.
3. treeModel.nodeStructureChanged(node); forces a node to be displayed.
4. treeModel.removeNodeFromParent(node); removes a node from the tree.
5. Node.getParent() gets the parent node.
6. (DefaultMutableTreeNode)tree.getLastSelectedPathComponent() can be
used to get the node which is selected
7. Tree.setSelectionPath(new TreePath(node.getPath()) sets the selected node.
8. Node.setUserObject(“new name”) changes the node’s text.
9. treeModel.nodeChanged(node); to force a modified node to display
10.Tree.expandPath(path) and Tree.collapse(path)
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);
} }
A Tabbed Panel
•
Instantiate a TabbedPane
–
•
Add a panel to the tabbed pane
–
–
•
JTabbedPane tabs = new JTabbedPane();
JPanel treeChanges = new TreeChangePanel();
tabs.addTab(“Modify”, treeChanges);
Set which pane shows initially
–
tabs.setSelectedIndex(0);
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);
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()=="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
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.
© Copyright 2026 Paperzz