Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6 1 Chapter 24 Advanced Swing.

Slides:



Advertisements
Similar presentations
15 Copyright © 2005, Oracle. All rights reserved. Adding User Interface Components and Event Handling.
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces.
Microsoft Visual Basic: Reloaded Chapter Seven More on the Repetition Structure.
Advanced Swing. Advanced Layout Managers GridBagLayout – Example: Web Browser (Grid Bag Layout)Web Browser (Grid Bag Layout) BoxLayout – Example: Web.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 36 JTable and JTree.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 16 Creating User Interfaces.
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 15 Creating User.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L03 (Chapter 15) Creating.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L04 (Chapter 15) Creating.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Creating User Interfaces. 2 Motivations A graphical user interface (GUI) makes a system user-friendly and easy to use. Creating a GUI requires creativity.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 16.1 Test-Driving the Flag Quiz Application.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 13 Creating User.
MVC and Swing IAT 351 Week 7 Lecture/tutorial Lyn Bartram.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 38 Advanced Java Database.
Swing, part 2 Tutorial 07 1 / 31 Leonid Barenboim 25/4/2010.
Tables CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
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.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 33 Advanced Java.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 19.1 Test-Driving the Shipping Hub Application.
Dale Roberts GUI Programming using Java - GUI Components Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Trees CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Java Programming: Advanced Topics 1 Components and Facilities for Rich Graphical User Interfaces Chapter 7.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 35 MVC and Swing MVC.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 15 Creating User.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 27 JavaBeans and.
CHAPTER:08 JAVA IDE PROGRAMMING-III Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
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.
Creating User Interfaces Chapter 13 CSCI CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons.
Creating User Interfaces Event-Driven Programming.
Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
Copyright © Curt Hill More Widgets In Abstract Window Toolbox.
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 32 JavaBeans and Bean.
Computer Science [3] Java Programming II - Laboratory Course Lab 4 -1 : Introduction to Graphical user interface GUI Components Faculty of Engineering.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
 Figure illustrates a hierarchy containing many event classes from the package java.awt.event.  Used with both AWT and Swing components.  Additional.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Address Book Application Introducing Database Programming.
1 Lecture 8: User Interface Components with Swing.
Notes for Assignment #2 Assn2: GUI Database Interface Corresponds with Chapters 28, 29, 31.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
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.
CompSci 230 S Software Construction
Advanced Swing Trees.
Advanced Swing Lists.
CompSci 280 S Introduction to Software Development
Advanced Swing Tables.
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
Chapter 7 Creating User Interfaces
עקרונות תכנות מונחה עצמים תרגול 8: MVC
Chapter 17 Creating User Interfaces
Chapter 17 Creating User Interfaces
Presentation transcript:

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 24 Advanced Swing Components

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Objectives · To understand the Swing model-view-controller architecture (§24.2). · To use JSpinner to scroll the next and previous values (§24.3). · To use JList to select single or multiple items in a list (§24.4). · To use JComboBox to select or edit a single item from a combo box (§24.5). · To use JTable to display and process tables (§24.6). · To use JTree to display data in a tree hierarchy (§24.7). · To create custom renderers for JSpinner, JList, JComboBox, JTable, and JTree (§24.2 – 24.7).

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Swing Model-View-Controller Architecture Each Swing user interface component (except some containers and dialog boxes such as JPanel, JSplitPane, JFileChooser, and JColorChooser) has a property named model that refers to its data model. The data model is defined in an interface whose name ends with Model. For example, the model for button component is ButtonModel. Most model interfaces have a default implementation class that is commonly named DefaultX, where X is its model interface name. For example, the default implementation class for ButtonModel is DefaultButtonModel.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Swing Components and Their Models For convenience, most Swing components contain some properties of their models, and these properties can be accessed and modified directly from the component without knowing the existence of the model. For example, the properties actionCommand and mnemonic are defined in both ButtonModel and JButton. Actually, these properties are in the AbstractButton class. Since JButton is a subclass of AbstractButton, JButton inherits all the properties from AbstractButton. It is unnecessary to use the models for the simple Swing components such as JButton, JToggleButton, JCheckBox, JRadioButton, JTextField, and JTextArea, because the frequently used properties in their models are also in these components. You can access and modify these properties directly through the components. For advanced components such as JSpinner, JList, JComboBox, JTable, and JTree, you have to work with their models to store, access and modify data.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved JSpinner A spinner is a text field with a pair of tiny arrow buttons on its right side that enable the user to select numbers, dates, or values from an ordered sequence, as shown in Figure The keyboard up/down arrow keys also cycle through the elements. The user may also be allowed to type a (legal) value directly into the spinner. A spinner is similar to a combo box, but a spinner is sometimes preferred because it doesn't require a drop down list that can obscure important data.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved The JSpinner Class A JSpinner's sequence value is defined by the SpinnerModel interface, which manages a potentially unbounded sequence of elements. The model doesn't support indexed random access to sequence elements. Only three sequence elements are accessible at a time: current, next and previous using the methods getValue(), getNextValue(), and getPreviousValue(), respectively.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Example 24.1 A Simple JSpinner Demo Problem: This example creates a JSpinner object for a sequence of numbers and displays the previous, current, and next number from the spinner on a label. SimpleSpinner Run NOTE: If you create a JSpinner object without specifying a model, the spinner displays a sequence of integers.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Spinner Models SpinnerModel is an interface for all spinner models. AbstractSpinnerModel is a convenience abstract class that implements SpinnerModel and provides the implementation for the registration/deregistration methods. SpinnerListModel, SpinnerNumberModel, and SpinnerDateModel are concrete implementations of SpinnerModel.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved SpinnerListModel SpinnerListModel (see Figure 24.6) is a simple implementation of SpinnerModel whose values are stored in a java.util.List.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved SpinnerNumberModel SpinnerNumberModel (see Figure 24.7) is a concrete implementation of SpinnerModel that represents a sequence of numbers. It contains the properties maximum, minimum, and stepSize.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved SpinnerDateModel SpinnerDateModel (see Figure 24.8) is a concrete implementation of SpinnerModel that represents a sequence of dates. The upper and lower bounds of the sequence are defined by properties called start and end and the size of the increase or decrease computed by the nextValue and previousValue methods is defined by a property called calendarField.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Example 24.2 Using Spinner Models and Editors Problem: This example uses a JSpinner component to display date and three separate JSpinner components to display day in a sequence of numbers, month in a sequence of strings, and year in a sequence of numbers, as shown in Figure All these four components are synchronized. For example, if you change year in the spinner for year, the date value in the date spinner is updated accordingly. SpinnerModelEditorDemo Run

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved JList JList has two supporting models: a list model and a list-selection model. The list model is for storing and processing data. The list-selection model is for selecting items. By default, items are rendered as strings or icons. You can also create a custom renderer implementing the ListCellRenderer interface.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved The JList Class

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved The layoutOrientation property JList jlst = new JList(); jlst.setLayoutOrientation(int);

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved The selectionMode property JList jlst = new JList(); jlst.setSelectionMode(int); JList.SINGLE_SELECTION JList.SINGLE_INTERVAL_SELECTION JList.MULTIPLE_INTERVAL_SELECTION

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Example 24.3 List Properties Demo Problem: This example creates a list of a fixed number of items displayed as strings. The example enables you to dynamically set visibleRowCount from a spinner, layoutOrientation from a combo box, and selectionMode from a combo box, as shown in Figure When you select one or more items, their values are displayed in a status label below the list. ListPropertiesDemo Run

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved List Models The JList class delegates the responsibilities of storing and maintaining data to its data model. The JList class itself does not have methods for adding or removing items from the list. These methods are supported in ListModel. DefaultListModel extends AbstractListModel and implements the two methods getSize and getElementAt, which are not implemented by AbstractListModel. AbstractListModel implements the registration methods in the ListModel, but does not implement the getSize and getElementAt methods.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Example 24.4 List Model Demo Problem: This example creates a list using a list model and allows the user to add and delete items in the list, as shown in Figure When the user clicks the Add new item button, an input dialog box is displayed to receive a new item. ListModelDemo Run

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved List Cell Renderer In addition to delegating data storage and processing to list models, JList delegates the rendering of the list cells to list cell renderers. All list cell renderers implement the ListCellRenderer interface, which defines a single method, getListCellRendererComponent, as follows By default, JList uses DefaultListCellRenderer to render its cells. The DefaultListCellRenderer class implements ListCellRenderer, extends JLabel, and can display either a string or an icon, but not both in the same cell. You can create a custom renderer by implementing ListCellRenderer.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Example 24.5 List Cell Renderer Demo Problem: This example creates a list of countries and displays the country flags and country names in the list, as shown in Figure When a country is selected in the list, its flag is displayed in a panel next to the list. ListCellRendererDemo Run MyListCellRenderer

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved JComboBox

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Combo Box Model JComboBox delegates the responsibilities of storing and maintaining data to its data model. All combo box models implement the ComboBoxModel interface, which extends the ListModel interface and defines the getSelectedItem and setSelectedItem methods for retrieving and setting a selected item. The methods for adding and removing items are defined in the MutableComboBoxModel interface DefaultComboBoxModel provides a concrete implementation for ComboModel.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Example 24.6 Combo Box Cell Renderer Demo Problem: This example creates a combo box that contains a list of countries and displays the country flags and country names in the list cell. When a country is selected in the list, its flag is displayed in a panel below the combo box. Run ComboBoxCellRenderer

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved JTable JTable is a Swing component that displays data in rows and columns in a two-dimensional grid.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved JTable and Its Supporting Models NOTE: All the supporting interfaces and classes for JTable are grouped in the javax.swing.table package.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved The JTable Class

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Example 24.7 Table Properties Demo Problem: This example demonstrates the use of several JTable properties. The example creates a table and allows the user to choose an Auto Resize Mode, specify the row height and margin, and indicate whether the grid is shown. Run TablePropertiesDemo

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Table Models JTable delegates data storing and processing to its table data model. A table data model must implement the TableModel interface, which defines the methods for registering table model listeners, manipulating cells, and obtaining row count, column count, column class, and column name. The AbstractTableModel class provides partial implementations for most of the methods in TableModel. It takes care of the management of listeners and provides some conveniences for generating TableModelEvents and dispatching them to the listeners. The DefaultTableModel provides concrete storage for data using a vector.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Table Column Model Table column models manage columns in a table. They can be used to select, add, move, and remove table columns. A table column model must implement the TableColumnModel interface, which defines the methods for registering table column model listeners, and for accessing and manipulating columns. DefaultTableColum nModel is a concrete class that implements TableColumnModel and stores its columns in a vector and contains an instance.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved The TableColumn Class The column model deals with all the columns in a table. The TableColumn class is used to model an individual column in the table. An instance of TableColumn for a specified column can be obtained using the getColumn(index) method in TableColumnModel or the getColumn(columnIdentifier) method in JTable.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved The JTableHeader Class JTableHeader is a GUI component that manages the header of the JTable (see Figure 24.29). When you create a JTable, an instance of JTableHeader is automatically created and stored in the tableHeader property.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Example 24.8 Modifying Rows and Columns Problem: This example demonstrates the use of table models, table column models, list-selection models, and the TableColumn class. The program allows the user to choose selection mode and selection type, to add or remove rows and columns, and to save, clear, and restore table. Run TableModelDemo

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Table Renderers and Editors Table cells are painted by cell renderers. By default, a cell object's string representation (toString()) is displayed and the string can be edited as it was in a text field. JTable maintains a set of predefined renderers and editors, listed in Table 24.1, which can be specified to replace default string renderers and editors. The predefined renderers and editors are automatically located and loaded to match the class returned from the getColumnClass() method in the table model. To use a predefined renderer or editor for a class other than String, you need to create your own table model by extending a subclass of TableModel. In your table model class, you need to override the getColumnClass() method to return the class of the column, as follows: public Class getColumnClass(int column) { return getValueAt(0, column).getClass(); } By default, all cells are editable. To prohibit a cell from being edited, override the isCellEditable(int rowIndex, int columnIndx) method in TableModel to return false. By default, this method returns true in AbstractTableModel.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Example 24.9 Using Predefined Table Renderers and Editors Problem: Write a program that displays a table for the books. The table consists of three rows with column names Title, Copies Needed, Publisher, Date Published, In-Stock, and Book Photo, as shown in Figure Display all the columns using the predefined renderers and editors. Assume dates and icons are not editable; prohibit users from editing of these two columns. Run MyTableModel TableCellRendererEditorDemo

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Custom Table Renderers and Editors Predefined renderers and editors are convenient and easy to use, but their functions are limited. The predefined image icon renderer displays the image icon in a label. The image icon cannot be scaled. If you want the whole image to fit in a cell, you need to create a custom renderer. A custom renderer can be created by extending the DefaultTableCellRenderer, which is a default implementation for the TableCellRender interface. The custom renderer must override the getTableCellRendererComponent() to return a component for rendering the table cell. The getTableCellRendererComponent() is defined as follows: public Component getTableCellRendererComponent (JTable table, Object value, boolean isSelected, boolean isFocused, int row, int column) This method signature is very similar to the getListCellRendererComponent() method used to create custom list cell renderers.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Example Using Custom Table Renderers and Editors Problem: Revise Example 24.9, “Using Predefined Table Renderers and Editors,” to display scaled image icons and to use a custom combo editor to edit the cells in the Publisher column. Run CustomTableCellRenderEditorDemo

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Table Events JTable does not fire table events. It fires the events such as MouseEvent, KeyEvent, and ComponentEvent inherited from its superclass JComponent. Table events are fired by table models, table column models, and table-selection models whenever changes are made to these models. Table models fire TableModelEvent when table data are changed. Table column models fire TableColumnModelEvent when columns are added, removed, or moved, or when the column selection changes. Table-selection models fire ListSelectionEvent when the selection changes.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Example Using Table Events Problem: This example demonstrates handling table events. The program displays messages on a text area when a row or a column is selected, when a cell is edited, or when a column is removed. Run TableEventDemo

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved JTree JTree is a Swing component that displays data in a treelike hierarchy. Root Leaf

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Tree Models While JTree displays the tree, the data representation of the tree is handled by TreeModel, TreeNode, and TreePath. TreeModel represents the entire tree, TreeNode represents a node, and TreePath represents a path to a node. Unlike the ListModel or TableModel, the tree model does not directly store or manage tree data. Tree data are stored and managed in TreeNode and TreePath. The TreeSelectionModel interface handles tree node selection. The DefaultTreeCellEditor can be used to edit the cells in a text field. The DefaultTreeCellRenderer class provides a default tree node renderer that can display a label and/or an icon in a node.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved The JTree Class

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Example Simple Tree Demo Problem: Write a program to create four trees: a default tree using the no-arg constructor, a tree created from an array of objects, a tree created from a vector, and a tree created from a hash table, as shown in Figure Enable the user to dynamically set the properties for rootVisible, rowHeight, and showsRootHandles. Run SimpleTreeDemo

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved TreeModel and DefaultTreeModel TreeModel contains the structural information about the tree, and tree data are stored and managed by TreeNode. DefaultTreeModel is a concrete implementation for TreeModel that uses TreeNodes.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved TreeNode, MutableTreeNode, and DefaultMutableTreeNode TreeNode stores models a single node in the tree. MutableTreeNode defines a subinterface of TreeNode with additional methods for changing the content of the node, for inserting and removing a child node, for setting a new parent, and for removing the node itself. DefaultMutableTreeNode is a concrete implementation of MutableTreeNode.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Example Tree Model Demo Problem: Write a program to create two trees that displays world, continents, countries and states. The two trees display identical contents. The program also displays the properties of the tree in a text area. Run TreeModelDemo

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved The TreePath Class The TreePath class represents a path from an ancestor to a descendant in a tree.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved TreeSelectionModel and DefaultTreeSelectionModel The selection of tree nodes is defined in the TreeSelectionModel interface. The DefaultTreeSelectionM odel class is a concrete implementation of the TreeSelectionModel, which maintains an array of TreePath objects representing the current selection.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Example Modifying Trees Problem: Write a program to create two trees that displays the same contents: world, continents, countries and states, as shown in Figure For the left tree on the left, enable the user to choose a selection mode, add a new child under the first selected node, and remove all the selected nodes. Run ProcessTree

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Tree Node Rendering DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer)jTree1.getCellRenderer(); renderer.setLeafIcon(yourCustomLeafImageIcon); renderer.setOpenIcon(yourCustomOpenImageIcon); renderer.setClosedIcon(yourCustomClosedImageIc on); renderer.setBackgroundSelectionColor(Color.red);

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Tree Editing // Customize editor JComboBox jcboColor = new JComboBox(); jcboColor.addItem("red"); jcboColor.addItem("green"); jcboColor.addItem("blue"); jcboColor.addItem("yellow"); jcboColor.addItem("orange"); jTree1.setCellEditor(new DefaultCellEditor(jcboColor)); jTree1.setEditable(true);

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Tree Rendering and Editing jTree1.setCellEditor (new DefaultTreeCellEditor(jTree1, new DefaultTreeCellRenderer(), new DefaultCellEditor(jcboColor)));

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Tree Events JTree can fire TreeSelectionEvent and TreeExpansionEvent, among many other events. Whenever a new node is selected, JTree fires a TreeSelectionEvent. Whenever a node is expanded or collapsed, JTree fires a TreeExpansionEvent. To handle the tree selection event, a listener must implement the TreeSelectionListener interface, which contains a single handler named valueChanged method. TreeExpansionListener contains two handlers named treeCollapsed and treeExpanded for handling node expansion or node closing.