JTree javax.swing.tree javax.swing.event. Components JTree and JTable COMPONENT – Model – Selection Model – Editor – Renderer.

Slides:



Advertisements
Similar presentations
Chapter 10: Data Structures II
Advertisements

Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
1 A Two-Level Binary Expression ‘-’ ‘8’ ‘5’ treePtr INORDER TRAVERSAL : has value 3 PREORDER TRAVERSAL: POSTORDER TRAVERSAL: 8 5 -
Michael Brockway Advanced Applications Development in Java Model-View-Controller Architecture l MVC Architecture l Delegate-Model Architecture l Observer.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 36 JTable and JTree.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
Main Index Contents 11 Main Index Contents Tree StructuresTree Structures (3 slides) Tree Structures Tree Node Level and Path Len. Tree Node Level and.
Trees. 2 Definition of a tree A tree is like a binary tree, except that a node may have any number of children Depending on the needs of the program,
Metric Converter What layout do we use? What is the class hierarchy? What listeners do we use? The code for this application is on our class web-site.
Trees. Definition of a tree A tree is like a binary tree, except that a node may have any number of children –Depending on the needs of the program, the.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
CHAPTER 12 Trees. 2 Tree Definition A tree is a non-linear structure, consisting of nodes and links Links: The links are represented by ordered pairs.
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
More on Trees CS1316: Representing Structure and Behavior.
Snap-Together Visualization Chris North Lab for Information Visualization and Evaluation Department of Computer Science Virginia Tech.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
UNCA CSCI September, 2001 These notes were prepared by the text’s author Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
Swing, part 2 Tutorial 07 1 / 31 Leonid Barenboim 25/4/2010.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Copyright © 2002 Systek The Adapter Pattern Overview And Experience Report Johannes Brodwall, Systek AS.
Trees CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Trees Isaac Sheff. Nodes Building blocks of trees “Parent” node may have “Child” nodes Can be both parent and child Can’t be its own ancestor Can’t have.
© 2006 Pearson Education Chapter 10: Non-linear Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Programming Practice 3 - Dynamic Data Structure
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 31 JTable and JTree.
Computer Graphics I, Fall 2008 Hierarchical Modeling II.
Swinging in Your Java Playground. Background Swing is a part of the Java Foundation Classes (JFC). The JFC is made up of features intended to give a programmer.
Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus.
CSC 213 – Large Scale Programming Lecture 10: Tree & Binary Tree Implementation.
Trees, Binary Search Trees, Balanced Trees, Graphs Graph Fundamentals Telerik Algo Academy
1 Lecture 21: Binary Search Tree delete etc. operations Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
More Swing utilities: JTree COMP204, Bernhard Pfahringer Code snippets taken from Sun’s JTree demo example.
עקרונות תכנות מונחה עצמים תרגול 8: MVC. Outline  MVC  Using the default models  Example- File Browser.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
Binary Search Trees (BST) Let’s look at some pics …and some code.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
Trees A non-linear implementation for collection classes.
Advanced Swing Trees. Contents I.Introduction to Trees II. Simple Trees III. Editing Trees and Tree Paths IV. Node Enumeration V. Rendering Nodes VI.
CompSci 230 S Software Construction
Chapter 12 – Data Structures
Non Linear Data Structure
Data Structures and Design in Java © Rick Mercer
Data Structures Binary Trees 1.
Binary Search Trees Chapter 7 Objectives
Planning & System installation
Nodes & Explorer Views Geertjan Wielenga Sun Microsystems.
Advanced Swing Trees.
Binary Search Tree (BST)
Section 8.1 Trees.
Trees.
Tonga Institute of Higher Education
Chapter 20: Binary Trees.
Chapter 10: Non-linear Data Structures
Chapter 21: Binary Trees.
Abstract Data Structures
עקרונות תכנות מונחה עצמים תרגול 8: MVC
Trees Definitions Implementation Traversals K-ary Trees
Metric Converter What layout do we use? What is the class hierarchy?
Chapter 10 1 – Binary Trees Tree Structures (3 slides)
Trees.
Chapter 20: Binary Trees.
Trees.
Presentation transcript:

JTree javax.swing.tree javax.swing.event

Components JTree and JTable COMPONENT – Model – Selection Model – Editor – Renderer

JTree Tree model – describes a JTree’s underlying data model. It specifies how a tree is mapped over a data structure. Tree selection Tree cell editor Tree cell renderer

Parts of trees Root Parent Node Branch Etc.

JTree The JTree class displays hierarchical data. It displays the data vertically. Each row has one item, called a “node.”

DefaultTreeModel Is a simple implementation of Tree Model that uses TreeNodes objects to hold items in the tree.

TreeModel – DefaultTreeModel TreeNode – DefaultMutableTreeNode

TreeModel methods getChild(Object parent, int index) getChildCount (Object parent) getIndexOfChild(Object parent, Object child) getRoot() isLeaf(Object node)

DefaultMutableTreeNode There is a rich set of methods in the DefaultMutableTreeNode class for viewing and manipulating nodes in a tree.

DefaultMutableTreeNode methods Tree enumeration preorderEnumeration() – Conducts a preorder traversal rooted at this node and returns an enumeration of TreeNode objects postorderEnumeration() – Conducts a postorder traversal rooted at this node and returns and enumeration of TreeNode objects

JTree sample code DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root"); DefaultMutableTreeNode subroot = new DefaultMutableTreeNode("Subroot"); DefaultMutableTreeNode leaf1 = new DefaultMutableTreeNode("Leaf 1"); DefaultMutableTreeNode leaf2 = new DefaultMutableTreeNode("Leaf 2");

Quick JTree code treeModel = new DefaultTreeModel(root); tree = new JTree(treeModel); treeModel.insertNodeInto(subroot, root, 0); treeModel.insertNodeInto(leaf1, subroot, 0); treeModel.insertNodeInto(leaf2, root, 1);

Customizing a Tree's Display Java, Windows, and Motif Look & Feel implementations

Add icons to tree nodes

Dynamically Changing a Tree tree = new JTree(treeModel); tree.setEditable(true); treeModel.addTreeModelListener(new MyTreeModelListener());... class MyTreeModelListener implements TreeModelListener { public void treeNodesChanged(TreeModelEvent e) { DefaultMutableTreeNode node; node = (DefaultMutableTreeNode)(e.getTreePath().getLastPa thComponent());

Homework Compile and run the following java programs: – TestTree.java – SampleTreeTest.java Links to the code will be found off the class website or

Tree node selection (tree listeners) tree.getSelectionModel().setSelectionMode (TreeSelectionModel.SINGLE_TREE_SELECTION); //Listen for when the selection changes. tree.addTreeSelectionListener(new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent e) { DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getLastSelectedP athComponent();