Advanced Swing Trees.

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Graphical User Interfaces
BackTracking Algorithms
Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Michael Brockway Advanced Applications Development in Java Model-View-Controller Architecture l MVC Architecture l Delegate-Model Architecture l Observer.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary trees Reading: L&C 9.1 – 9.7.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 36 JTable and JTree.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter Trees and B-Trees.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 26 Binary Search Trees.
© Marty Hall, Larry Brown Web core programming 1 Advanced Swing Custom Data Models and Cell Renderers.
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
CS 206 Introduction to Computer Science II 09 / 30 / 2009 Instructor: Michael Eckmann.
CIS 068 Welcome to CIS 083 ! Introduction to GUIs: JAVA Swing.
MVC and Swing IAT 351 Week 7 Lecture/tutorial Lyn Bartram.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Created on 29/10/2008yahaya.wordpress.com1 Trees Another common nonlinear data structure is the tree. We have already seen an example of a tree when we.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 – Graphical User Interfaces Part 2 Outline.
Swing, part 2 Tutorial 07 1 / 31 Leonid Barenboim 25/4/2010.
CSE 219 Patterns in Programming More Design Patterns.
data ordered along paths from root to leaf
Java Swing. Swing is a set of classes that provides more powerful and flexible components than are possible with the AWT. In addition to the familiar.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 24 Advanced Swing.
Copyright © 2002 Systek The Adapter Pattern Overview And Experience Report Johannes Brodwall, Systek AS.
Priority Queues Dr. David Matuszek
Trees CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 35 MVC and Swing MVC.
JTree javax.swing.tree javax.swing.event. Components JTree and JTable COMPONENT – Model – Selection Model – Editor – Renderer.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 25 Trees, Iterators,
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 31 JTable and JTree.
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.
Chapter 131 Applets and HTML Chapter Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
Coming up: Inheritance
1 Chapter 24 Advanced Swing Components. 2 Objectives · To understand the Swing model-view-controller architecture (§24.2). · To use JSpinner to scroll.
עקרונות תכנות מונחה עצמים תרגול 8: MVC. Outline  MVC  Using the default models  Example- File Browser.
Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
Advanced Swing Custom Data Models and Cell Renderers.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
Lecture 33: More Graphical User Interface (GUI) Announcements & Review Read Ch GU1 & GU2 Cohoon & Davidson Ch 14 Reges & Stepp Lab 10 set game due 4/26.
Graphical User Interfaces Part 2 1 Outline TreeViews ListViews Tab Control.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
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 First Look at GUI Applications Radio Buttons and Check Boxes
A First Look at GUI Applications
Advanced Swing Lists.
Haotian Tang Benjie Miao
Dealing with more complex models
Advanced Swing Tables.
Chapter 8: Data Abstractions
Graphical User Interfaces -- Introduction
AWT Components and Containers
Chapter 8: Data Abstractions
עקרונות תכנות מונחה עצמים תרגול 8: MVC
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
1/10/2019 JavaFX Events COSC 330.
Binary Search Trees.
Metric Converter What layout do we use? What is the class hierarchy?
Java Programming: Advanced Topics
Red-Black Implementation of 2-3 Trees
Graphical User Interface
Heaps.
Presentation transcript:

Advanced Swing Trees

Contents Introduction to Trees Simple Trees Editing Trees and Tree Paths Node Enumeration Rendering Nodes Listening to Tree Events

I. Introduction To Trees

Tree Terminology

Tree Traversal Orders

Java Tree Classes

II. Simple Trees To construct a JTree, supply the tree model in the constructor: TreeModel model = . . .; JTree tree = new JTree(model); How do you obtain a tree model? Creating a class that implements the TreeModel interface. Using the DefaultTreeModel that the Swing library supplies. To construct a default tree model, you must supply a root node. TreeNode root = . . .; DefaultTreeModel model = new DefaultTreeModel(root);

How do you obtain a tree node? Implement the TreeNode interface. Using the DefaultMutableTreeNode. This class implements the MutableTreeNode interface, a subinterface of TreeNode A default mutable tree node holds an object, the user object. The tree renders the user objects for all nodes. Unless you specify a renderer, the tree simply displays the string that is the result of the toString method.

Setting User Objects You can specify the user object in the constructor, DefaultMutableTreeNode node = new DefaultMutableTreeNode("Texas"); or you can set it later with the setUserObject method. . . . node.setUserObject("California");

Establishing the Parent/Child Relationships Start with the root node, and use the add method to add the children: DefaultMutableTreeNode root = new DefaultMutableTreeNode("World"); DefaultMutableTreeNode country = new DefaultMutableTreeNode("USA"); root.add(country); DefaultMutableTreeNode state = new DefaultMutableTreeNode("California"); country.add(state);

Constructing a JTree With a Root Node Construct a JTree with the tree model DefaultTreeModel treeModel = new DefaultTreeModel(root); JTree tree = new JTree(treeModel); Construct a JTree with a root node JTree tree = new JTree(root); Then the tree automatically constructs a default tree model.

III. Editing Trees and Tree Paths The JTree class has a way of identifying nodes in a tree. It does not deal with tree nodes, but with paths of objects, called tree paths. A tree path starts at the root and consists of a sequence of child nodes The TreePath class manages a sequence of Object (not TreeNode!) references.

Getting the Selected Node TreePath selectionPath = tree.getSelectionPath(); DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) selectionPath.getLastPathComponent(); There is a convenience method that gives the selected node immediately: DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();

Adding A New Node Insert a new node Remove a node void insertNodeInto(MutableTreeNode newChild, MutableTreeNode parent, int index) Remove a node void removeNodeFromParent(MutableTreeNode node) Notify the tree model listeners that node has changed void nodeChanged(TreeNode node)

Making a New Node Visible To expand all parent nodes so that the newly added node becomes visible TreeNode[] nodes = model.getPathToRoot(newNode); TreePath path = new TreePath(nodes); tree.makeVisible(path); To expand all nodes along the path, and scroll the node at the end of the path into view tree.scrollPathToVisible(path);

tree.setEditable(true); By default, tree nodes cannot be edited. To allow the user to edit a node tree.setEditable(true);

Editing a Tree

IV. Node Enumeration To find a node in a tree by starting at the root and visiting all children until you have found a match

Here is the typical usage pattern: Enumeration breadthFirst = node.breadthFirstEnumeration(); while (breadthFirst.hasMoreElements()) do something with breadthFirst.nextElement(); Enumeration breadthFirstEnumeration() Enumeration depthFirstEnumeration()

V. Rendering Nodes To change the way in which a tree component draws the nodes. All these changes are made possible by installing a new tree cell renderer into the tree. By default, the JTree class uses DefaultTreeCellRenderer objects to draw each node. The DefaultTreeCellRenderer class extends the JLabel class. The label contains the node icon and the node label.

You can customize the display in three ways. You can change the icons, font, and background color used by a DefaultTreeCellRenderer. These settings are used for all nodes in the tree. You can install a renderer that extends the DefaultTreeCellRenderer class and vary the icons, fonts, and background color for each node. You can install a renderer that implements the TreeCellRenderer interface, to draw a custom image for each node.

1. Using a DefaultTreeCellRenderer object DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer(); renderer.setLeafIcon(new ImageIcon( "blue-ball.gif")); // used for leaf nodes renderer.setClosedIcon(new ImageIcon( "red-ball.gif")); // used for collapsed nodes renderer.setOpenIcon(new ImageIcon( "yellow-ball.gif")); // used for expanded nodes tree.setCellRenderer(renderer);

2. Extending the DefaultTreeCellRenderer class class MyTreeCellRenderer extends DefaultTreeCellRenderer{ public Component getTreeCellRendererComponent( JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus){ super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus); DefaultMutableTreeNode node = (DefaultMutableTreeNode) value; look at node.getUserObject(); Font font = appropriate font; setFont(font); return this; } };

V. Listening to Tree Events public TreeSelectionListener interface { void valueChanged(TreeSelectionEvent event) } That method is called whenever the user selects or deselects tree nodes. Adding a tree selection listener to a tree tree.addTreeSelectionListener(listener);

Tree Selection Modes The JTree class uses a TreeSelectionModel to manage node selection. TreeSelectionModel.SINGLE_TREE_SELECTION TreeSelectionModel.CONTIGUOUS_TREE_SELECTION TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION For example: tree.getSelectionModel().setSelectionMode( TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);

Class Browser