Java Reference
In-Depth Information
Example 10•19: PropertyTable.java (continued)
* This method returns the value that appears at the specified row and
* column of the table
**/
public Object getValueAt(int row, int column) {
PropertyDescriptor prop = properties[row];
switch(column) {
case 0: return prop.getName();
case 1: return prop.getPropertyType();
case 2: return getAccessType(prop);
case 3: return new Boolean(prop.isBound());
default: return null;
}
}
// A helper method called from getValueAt() above
String getAccessType(PropertyDescriptor prop) {
java.lang.reflect.Method reader = prop.getReadMethod();
java.lang.reflect.Method writer = prop.getWriteMethod();
if ((reader != null) && (writer != null)) return "Read/Write";
else if (reader != null) return "Read-Only";
else if (writer != null) return "Write-Only";
else return "No Access"; // should never happen
}
}
}
Displaying Trees
The JTree component is used to display tree-structured data. If your data is in the
form of nested arrays, vectors, or hashtables, you can simply pass the root node of
the data structure to the JTree constructor, and it displays it. Tree data is not typi-
cally in this form, however. In order to display data that is in another form, you
must implement the javax.swing.Tree.TreeModel interface to interpret the data in
a way the JTree component can use it.
Example 10-20 shows a listing of ComponentTree.java ,a JTree subclass that uses
a custom TreeModel implementation to display the containment hierarchy of an
AWT or Swing GUI in tree form. The class includes a main() method that uses the
ComponentTree class to display its own component hierarchy, as shown in Figure
10-16. As with the previous JTable example, the key to this example is the
TreeModel implementation. The main() method also illustrates a technique for
responding to tree node selection events.
Example 10•20: ComponentTree.java
package com.davidflanagan.examples.gui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
/**
* This class is a JTree subclass that displays the tree of AWT or Swing
* component that make up a GUI.
Search WWH ::




Custom Search