sun网站上有表格树的实现:
网址:http://java.sun.com/products/jfc/tsc/articles/treetable1/
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
Creating TreeTables in Swing
![]() |
Creating TreeTablesin Swing Note: please also see part2 and part3 for further updates of TreeTable By Philip Milne A TreeTable is a combinationof a Tree and a Table -- a component capable of both expanding and contractingrows, as well as showing multiple columns of data. The Swing package doesnot contain a JTreeTable component, but it is fairly easy to create one byinstalling a JTree as a renderer for the cells in a JTable. This article explains how to use this technique to create a TreeTable. Itconcludes with a example application, named TreeTableExample0, whichdisplays a working TreeTable browser that you can use to browse a local filesystem (see illustration). In Swing, the JTree, JTable, JList, and JComboBox components use a singledelegate object called a cell renderer to draw their contents. Acell renderer is a component whose paint() method is used to draweach item in a list, each node in a tree, or each cell in a table. Acell renderer component can be viewed as a "rubber stamp": it'smoved into each cell location using setBounds(), and is then drawnwith the component's paint() method. By using a component to render cells, you can achieve the effect of displayinga large number of components for the cost of creating just one. Bydefault, the Swing components that employ cell renderers simply use a JLabel,which supports the drawing of simple combinations of text and an icon.To use any Swing component as a cell renderer, all you have to do is createa subclass that implements the appropriate cell renderer interface: TableCellRendererfor JTable, ListCellRenderer for JList, and so on.
Rendering in SwingHere's an example of how you can extend a JCheckBox to act as a rendererin a JTable:
How the example program worksThe code snippet shown above -- part of a sample program presented infull later in this article -- shows how to use a JTree as a renderer insidea JTable. This is a slightly unusual case because it uses the JTree topaint a single node in each cell of the table rather than painting a completecopy of the tree in each of the cells.
As each cell is painted, the JTable goes through the usual process ofgetting the renderer, setting its bounds, and asking it to paint. In thiscase, though, we record the row number of the cell being painted in aninstance variable named visibleRow.We also override setBounds(),so that the JTree remains the same height as the JTable, despite the JTable'sattempts to set its bounds to fit the dimensions of the cell being painted. To complete this technique we override paint(),making use of the stored variable visibleRow,an operation that effectively moves the clipping rectangle over the appropriatepart of the tree. The result is that the JTree draws just one of its nodeseach time the table requests it to paint. In addition to installing the JTree as a renderer for the cells in thefirst column, we install the JTree as the editor for these cells also.The effect of this strategy is the JTable then passes all mouse and keyboardevents to this "editor" -- thus allowing the tree to expand andcontract its nodes as a result of user input.
Example: A file-systembrowserThe example program presented with this article creates and implementsa browser for a file system. Each directory can be expanded and collapsed.Other columns in the table display important properties of files and directories,such as file sizes and dates.
Here is the full list of classes used in the example program, along witha brief description of what each class does (you can download or view eachfile by clicking its link):
CaveatsFor clarity, we have kept the TreeTableExample0 program short. To be complete,a full-featured JTreeTable component would have to deal with a number ofother issues. Here are some features that are missing from this chapter'ssample program:
| ![]() |
![]() |
![]() |