FileDocCategorySizeDatePackage
GenericListCellRenderer.javaAPI DocExample1640Mon Jan 09 11:01:58 GMT 2006None

GenericListCellRenderer

public class GenericListCellRenderer extends DefaultListCellRenderer

Fields Summary
protected String
method
Constructors Summary
public GenericListCellRenderer(String method)

        super();
        this.method = method;
    
Methods Summary
public java.awt.ComponentgetListCellRendererComponent(javax.swing.JList list, java.lang.Object value, int index, boolean isSelected, boolean cellHasFocus)

        
        JLabel label = (JLabel)super.getListCellRendererComponent(
            list,value,index, isSelected, cellHasFocus);
        
        try {
            Method meth = value.getClass().getMethod(method,null);
            if(meth != null) {
                Object retval = meth.invoke(value,null);
                label.setText(""+retval);
            }
        } catch (Exception ex) {
            System.out.println("got an execption: " + ex);
            ex.printStackTrace();
        }
        
        return label;
        
    
public static voidmain(java.lang.String[] args)

        String[] data = { "Proton", "Neutron", "Electron" };
        JList list = new JList(data);
        
//        GenericListCellRenderer renderer = 
//            new GenericListCellRenderer("toString");
        GenericListCellRenderer renderer = 
            new GenericListCellRenderer("length");
//        GenericListCellRenderer renderer = 
//            new GenericListCellRenderer("hashCode");
        list.setCellRenderer(renderer);
        
        JFrame frame = new JFrame("Cell Renderer Hack");
        frame.getContentPane().add(list);
        frame.pack();
        frame.setVisible(true);