Java Reference
In-Depth Information
Example 10•19: PropertyTable.java (continued)
import java.beans.*;
// For JavaBean introspection
import java.util.*;
// For array sorting
/**
* This class is a JTable subclass that displays a table of the JavaBeans
* properties of any specified class.
**/
public class PropertyTable extends JTable {
/** This main method allows the class to be demonstrated standalone */
public static void main(String[] args) {
// Specify the name of the class as a command-line argument
Class beanClass = null;
try {
// Use reflection to get the Class from the classname
beanClass = Class.forName(args[0]);
}
catch (Exception e) { // Report errors
System.out.println("Can't find specified class: "+e.getMessage());
System.out.println("Usage: java TableDemo <JavaBean class name>");
System.exit(0);
}
// Create a table to display the properties of the specified class
JTable table = new PropertyTable(beanClass);
// Then put the table in a scrolling window, put the scrolling
// window into a frame, and pop it all up on to the screen
JScrollPane scrollpane = new JScrollPane(table);
JFrame frame = new JFrame("Properties of JavaBean: " + args[0]);
frame.getContentPane().add(scrollpane);
frame.setSize(500, 400);
frame.setVisible(true);
}
/**
* This constructor method specifies what data the table will display
* (the table model) and uses the TableColumnModel to customize the
* way that the table displays it. The hard work is done by the
* TableModel implementation below.
**/
public PropertyTable(Class beanClass) {
// Set the data model for this table
try {
setModel(new JavaBeanPropertyTableModel(beanClass));
}
catch (IntrospectionException e) {
System.err.println("WARNING: can't introspect: " + beanClass);
}
// Tweak the appearance of the table by manipulating its column model
TableColumnModel colmodel = getColumnModel();
// Set column widths
colmodel.getColumn(0).setPreferredWidth(125);
colmodel.getColumn(1).setPreferredWidth(200);
colmodel.getColumn(2).setPreferredWidth(75);
colmodel.getColumn(3).setPreferredWidth(50);
Search WWH ::




Custom Search