Trees CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.

Slides:



Advertisements
Similar presentations
User Interfaces II GUI – Awt, Swing, Web
Advertisements

Graphics CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
User Interface Design CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
March Ron McFadyen1 Composite Used to compose objects into tree structures to represent part-whole hierarchies Composite lets clients treat.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 36 JTable and JTree.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Agenda –interfaces and realization –type hierarchy –introduction to graphics and event handling.
What is Covered Everything up to and including Lecture 11 Types Recursion (including grammars) Lists and Trees GUIs Does not include Big-O notation (Lecture.
GUI Tutorial Day 3. Custom Dialog Create, display, hide, retrieve information.
© L.Lúcio, An example GUI in Java n Two graphic libraries in Java u AWT u Swing n Swing is more recent than AWT: u Built on top of AWT classes;
Unit 131 GUI Layout Managers Learning Outcomes oList and distinguish between the four most common, standard layout managers in Java. oUse these and other.
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.
Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.
Garbage Collection CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
OOP (Java): Layout/ OOP Objectives – –describe the basic layout managers for GUIs Semester 2, GUI Layout.
Methods CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
CSE 219 Computer Science III Graphical User Interface.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
Swing, part 2 Tutorial 07 1 / 31 Leonid Barenboim 25/4/2010.
Generics CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
CSE 219 Patterns in Programming More Design Patterns.
Tables CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 24 Advanced Swing.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Copyright © 2002 Systek The Adapter Pattern Overview And Experience Report Johannes Brodwall, Systek AS.
JFrame and JPanel CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
Java's Graphical User Interface Toolkit
JDBC Tutorial CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Lecture 19 – Swing Control Programming Lecturer: Prof Jim Warren.
JTree javax.swing.tree javax.swing.event. Components JTree and JTable COMPONENT – Model – Selection Model – Editor – Renderer.
Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
 GUI – Graphic User Interface  Up to now in the programs we have written all output has been sent to the standard output device i.e.: the DOS console.
Layout Managers CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Option Panes CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 31 JTable and JTree.
Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
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.
Monitors CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
COMPOSITE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract factory.
1 Chapter 24 Advanced Swing Components. 2 Objectives · To understand the Swing model-view-controller architecture (§24.2). · To use JSpinner to scroll.
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.
GUI Components CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Event-Driven Programming CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Semaphores CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Classes CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Graphical User Interface (GUI)
Menus and Toolbars CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Thread Pools CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Errors, GUIs, and Patterns Dr. Doug Twitchell August 30, 2005.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
Dept. of CSIE, National University of Tainan 10/21/2012 Arranging Components on a User Interface.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
GUI Programming in Java Hao Jiang Boston College April, 2009.
Advanced Swing Trees. Contents I.Introduction to Trees II. Simple Trees III. Editing Trees and Tree Paths IV. Node Enumeration V. Rendering Nodes VI.
A Quick Java Swing Tutorial
Graphical User Interfaces
Java GUI.
Nodes & Explorer Views Geertjan Wielenga Sun Microsystems.
Advanced Swing Trees.
A Quick Java Swing Tutorial
עקרונות תכנות מונחה עצמים תרגול 8: MVC
Metric Converter What layout do we use? What is the class hierarchy?
A Quick Java Swing Tutorial
어서와 Java는 처음이지! 제13장 실전프로젝트.
Presentation transcript:

Trees CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Outline USC CSCI 201L2/10 ▪T▪Trees ▪P▪Program

Trees Overview ▪JTree is a component that displays data in a treelike hierarchy ▪All nodes displayed in the tree are in the form of a hierarchical indexed list ›A node can have child nodes ›A node with no children is a leaf ›A node with no parent is the root ▪TreeModel represents the entire tree ▪TreeNode represents a node ▪TreePath represents a path to a node ▪TreeModel does not directly store or manage tree data (different from the TableModel with JTables) ▪MutableTreeNode represents a tree node that can be mutated by adding or removing child nodes or changing the content of the node ▪TreeSelectionModel handles the tree node selection USC CSCI 201L3/10 Trees

JTree Hierarchy USC CSCI 201L4/10 Trees

JTree API USC CSCI 201L5/10 Tables

JTree Example 1 import javax.swing.JFrame; 2 import javax.swing.JPanel; 3 import javax.swing.JScrollPane; 4 import javax.swing.JTree; 5 6 public class Test extends JFrame { 7 public Test() { 8 super("Tree Example"); 9 10 JPanel jp = new JPanel(); 11 String [] data = {"Cat", "Dog", "Fish", "Frog", "Salamander"}; JTree tree = new JTree(data); 14 tree.setRootVisible(true); 15 JScrollPane jsp = new JScrollPane(tree); 16 jp.add(jsp); 17 add(jp); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 20 setSize(500, 200); 21 setLocationRelativeTo(null); 22 setVisible(true); 23 } public static void main(String args[]) { 26 Test t = new Test(); 27 } 28 } USC CSCI 201L6/10 Trees

Tree Model Example 1 // import statements omitted 2 3 public class Test extends JFrame { 4 public Test() { 5 super("Tree Example"); 6 7 JPanel jp = new JPanel(); 8 DefaultMutableTreeNode root, europe, northAmerica, us; 9 europe = new DefaultMutableTreeNode("Europe"); 10 europe.add(new DefaultMutableTreeNode("UK")); 11 europe.add(new DefaultMutableTreeNode("Germany")); 12 europe.add(new DefaultMutableTreeNode("France")); 13 europe.add(new DefaultMutableTreeNode("Norway")); northAmerica = new DefaultMutableTreeNode("North America"); 16 us = new DefaultMutableTreeNode("US"); 17 us.add(new DefaultMutableTreeNode("California")); 18 us.add(new DefaultMutableTreeNode("Texas")); 19 us.add(new DefaultMutableTreeNode("New York")); 20 us.add(new DefaultMutableTreeNode("Florida")); 21 us.add(new DefaultMutableTreeNode("Illinois")); 22 northAmerica.add(us); 23 northAmerica.add(new DefaultMutableTreeNode("Canada")); root = new DefaultMutableTreeNode("World"); 26 root.add(europe); 27 root.add(northAmerica); JPanel panel = new JPanel(); 30 panel.setLayout(new GridLayout(1, 2)); 31 // next two lines create different instances of trees with the same nodes 32 panel.add(new JScrollPane(new JTree(root))); 33 panel.add(new JScrollPane(new JTree(new DefaultTreeModel(root)))); USC CSCI 201L7/10 Trees 34 JTextArea jtaMessage = new JTextArea(); 35 jtaMessage.setWrapStyleWord(true); 36 jtaMessage.setLineWrap(true); 37 add(new JSplitPane(JSplitPane.VERTICAL_SPLIT, panel, new JScrollPane(jtaMessage)), BorderLayout.CENTER); // Get tree information 40 jtaMessage.append("Depth of the node US is " + us.getDepth()); 41 jtaMessage.append("\nLevel of the node US is " + us.getLevel()); 42 jtaMessage.append("\nFirst child of the root is " + root.getFirstChild()); 43 jtaMessage.append("\nFirst leaf of the root is " + root.getFirstLeaf()); 44 jtaMessage.append("\nNumber of the children of the root is " + root.getChildCount()); 45 jtaMessage.append("\nNumber of leaves in the tree is " + root.getLeafCount()); 46 String breadthFirstSearchResult = ""; // Breadth-first traversal 49 Enumeration bf = root.breadthFirstEnumeration(); 50 while (bf.hasMoreElements()) { 51 breadthFirstSearchResult += bf.nextElement().toString() + " "; 52 } 53 jtaMessage.append("\nBreath-first traversal from the root is " + breadthFirstSearchResult); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 56 setSize(500, 200); 57 setLocationRelativeTo(null); 58 setVisible(true); 59 } public static void main(String args[]) { 62 Test t = new Test(); 63 } 64 }

More Tree Details ▪JTree can fire TreeSelectionEvent and TreeExpansionEvent ›When a new node is selected, a TreeSelectionEvent is fired To handle the TreeSelectionEvent, a listener must implement the TreeSelectionListener interface ›When a node is expanded or collapsed, a TreeExpansionEvent is fired To handle the TreeExpansionEvent, a listener must implement the TreeExpansionListener interface (or extend TreeExpansionAdapter) USC CSCI 201L8/25 Trees

Outline USC CSCI 201L9/10 ▪T▪Trees ▪P▪Program

Program ▪Create the following GUI. USC CSCI 201L10/10 Program