Java Reference
In-Depth Information
Example 10•19: PropertyTable.java (continued)
// Right justify the text in the first column
TableColumn namecol = colmodel.getColumn(0);
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
renderer.setHorizontalAlignment(SwingConstants.RIGHT);
namecol.setCellRenderer(renderer);
}
/**
* This class implements TableModel and represents JavaBeans property data
* in a way that the JTable component can display. If you've got some
* type of tabular data to display, implement a TableModel class to
* describe that data, and the JTable component will be able to display it.
**/
static class JavaBeanPropertyTableModel extends AbstractTableModel {
PropertyDescriptor[] properties; // The properties to display
/**
* The constructor: use the JavaBeans introspector mechanism to get
* information about all the properties of a bean. Once we've got
* this information, the other methods will interpret it for JTable.
**/
public JavaBeanPropertyTableModel(Class beanClass)
throws java.beans.IntrospectionException
{
// Use the introspector class to get "bean info" about the class.
BeanInfo beaninfo = Introspector.getBeanInfo(beanClass);
// Get the property descriptors from that BeanInfo class
properties = beaninfo.getPropertyDescriptors();
// Now do a case-insensitive sort by property name
// The anonymous Comparator implementation specifies how to
// sort PropertyDescriptor objects by name
Arrays.sort(properties, new Comparator() {
public int compare(Object p, Object q) {
PropertyDescriptor a = (PropertyDescriptor) p;
PropertyDescriptor b = (PropertyDescriptor) q;
return a.getName().compareToIgnoreCase(b.getName());
}
public boolean equals(Object o) { return o == this; }
});
}
// These are the names of the columns represented by this TableModel
static final String[] columnNames = new String[] {
"Name", "Type", "Access", "Bound"
};
// These are the types of the columns represented by this TableModel
static final Class[] columnTypes = new Class[] {
String.class, Class.class, String.class, Boolean.class
};
// These simple methods return basic information about the table
public int getColumnCount() { return columnNames.length; }
public int getRowCount() { return properties.length; }
public String getColumnName(int column) { return columnNames[column]; }
public Class getColumnClass(int column) { return columnTypes[column]; }
/**
Search WWH ::




Custom Search