![]() ![]() ![]() |
14
Tree/Table Components
Features of JCTreeExplorer and JCTreeTable
Classes and Interfaces
Properties
Examples
14.1 Features of JCTreeExplorer and JCTreeTable
Swing's
JTree
andJTable
are the two components that do more than merely display data; they attempt to manage the data as well. This becomes important when you need to organize large amounts of data and provide a view that displays a portion of it along with an indication of its relationship to the rest. Information that has a hierarchical structure, like a file system, can be displayed as tree data, while other types of data nicely fit a tabular format. There are a large number of data structures that combine tree-like and table-like properties. A file system has a hierarchical organization that begs to be represented as a tree, yet the individual directories and files have properties, such as name, size, type, and date modified, that fit nicely in a row-column organization. Obviously there is a need for a component that lets you combine the look and functionality of both a tree and a table.
JCTreeExplorer
andJCTreeTable
fill the need for components that have the dual characteristics of a tree and a table, and provides these functions:
- Allows you to view the object as a tree, with an accompanying table. Any tree node may have tabular data associated with it.
- Contains a flexible "painter" object that accommodates a Swing cell renderer or a JClass cell renderer.
- Permits the construction of arbitrary data sources as treetables through its
JCTreeTableModel
interface. Any class can be used to supply data to the treetable, provided that it implements theJCTreeTableModel
interface.- The two components have advanced column sorting functionality. Each column can have a different ordered set of columns that are to be used as secondary sort keys. When a user clicks on a column header to sort that column, any identical cells are arranged based on the sort order of the secondary key, or keys.
- Cells may be edited by implementing
JCCellEditor
.- Folder icons can be customized by replacing the editor/renderer, or by setting a
JCIconRenderer
.Since
JCTreeExplorer
andJCTreeTable
are enhancements of Swing'sJTable
andJTree
, it's a good idea to be familiar with those components to ease the learning curve. Need a primer on Swing's table and tree components? See the tutorial on How to Use Tables and How to Use Trees at Sun's javasoft Web site.
JCTreeExplorer
presents a table for the currently selected node, whileJCTreeTable
shows table rows for all visible nodes. See the following figure for the different visual characteristics between the two components.
Figure 44 : Views of a JCTreeTable and a JCTreeExplorer.
JCTreeTable
andJCTreeExplorer
support Swing's pluggable look-and-feel. The previous figure shows the Windows look and feel, while the following two figures show the default Metal look and feel.
Figure 45 : JCTreeTable component, metal look and feel.
Figure 46 : JCTreeExplorer component, metal look and feel.
14.2 Classes and Interfaces
Interfaces for JCTreeExplorer and JCTreeTable
Here is the definition of the
public interface JCTreeTableModel {JCTreeTableModel
interface:
// Returns the value of the specific node and column
public Object getValueAt(Object node, int column);
// Returns whether a particular cell is editable, given the node and column
public boolean isCellEditable(Object node, int column);
// Sets the value at a particular node and column
public void setValueAt(Object value, Object node, int column);
// The following methods map exactly onto javax.swing.table.TableModel
public void addTableModelListener(TableModelListener l);
public Class getColumnClass(int column);
public int getColumnCount();
public String getColumnName(int column);
public int getRowCount();
public Object getValueAt(int row, int column);
public boolean isCellEditable(int row, int column);
public void removeTableModelListener(TableModelListener l);
public void setValueAt(Object value, int row, int column);
// The following methods map exactly onto javax.swing.tree.TreeModel
public void addTreeModelListener(TreeModelListener l);
public Object getChild(Object parent, int index);
public int getChildCount(Object parent);
public int getIndexOfChild(Object parent, Object child);
public Object getRoot();
public boolean isLeaf(Object node);
public void removeTreeModelListener(TreeModelListener l);
public void valueForPathChanged(TreePath path, Object newValue);
}Classes
Providing your own sorting mechanism
If you need to provide your own sorting algorithm, one way is to subclass
JCRowComparator
and pass a comparator of the new type toTreeWithSortableChildren
.
14.3 Properties
For a complete list of properties, please see Properties of JCTreeExplorer and Properties of JCTreeTable in Appendix A.
Properties of JCTreeExplorer
Properties of JCTreeTable
14.4 Methods
JCTreeExplorer Methods
JCTreeTable Methods
14.5 Examples
Implementing a custom node icon for a JCTreeTable
Your application may require that you supply your own custom node icon for the tree view. Create your own implementation of
public Icon getNodeIcon(TreeModel treemodel,JCTreeIconRenderer
, and write a method similar to the one whose signature is shown here:
Object node,
Object value,
Class object_class,
boolean is_leaf,
boolean is_expanded,
Icon plaf_icon)You may want to have two icons, one for nodes with children and one for leaf elements. In that case, use the
boolean
parameteris_leaf
to choose which icon will be used.The method should return the
Icon
you want to use. Pass your implementation ofJCTreeIconRenderer
to your instance of aJCTreeTable
usingsetTreeIconRenderer(JCTreeIconRenderer renderer)
.Please see TreeExplorer.java and TreeTable.java in the JCLASS_HOME\examples\elements\ directory for some examples of using both
JCTreeExplorer
andJCTreeTable
.
![]() ![]() ![]() |