FileDocCategorySizeDatePackage
PropertyTable.javaAPI DocExample6153Sat Jan 24 10:44:34 GMT 2004je3.gui

PropertyTable

public class PropertyTable extends JTable
This class is a JTable subclass that displays a table of the JavaBeans properties of any specified class.

Fields Summary
Constructors Summary
public PropertyTable(Class beanClass)
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.

	// 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);
	
	// Right justify the text in the first column
	TableColumn namecol = colmodel.getColumn(0);
	DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
	renderer.setHorizontalAlignment(SwingConstants.RIGHT);
	namecol.setCellRenderer(renderer);
    
Methods Summary
public static voidmain(java.lang.String[] args)
This main method allows the class to be demonstrated standalone

	// 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 PropertyTable <bean 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);