HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Reflecting a Bean onto a Table"
Volume: 4 Issue: 8, p. 38

	

Listing 1: 

import com.sun.java.swing.*; 
import com.sun.java.swing.table.*; 
import java.util.*; 

public class ComponentInspectorTableModel extends DefaultTableModel{ 

public ComponentInspectorTableModel(){ 
  super(); 
  } 

public ComponentInspectorTableModel(int numColumns, 
int numRows){ 
    super(numColumns, numRows); 
  } 

public ComponentInspectorTableModel(Object[] columns, 
int numRows){ 
    super(columns, numRows); 
  } 

public ComponentInspectorTableModel(Object[][] columns, Object[] rows){ 
    super(columns, rows); 
  } 

public ComponentInspectorTableModel(Vector columns, 
int numRows){ 
    super(columns, numRows); 
  } 

public ComponentInspectorTableModel(Vector columns, 
Vector rows){ 
    super(columns, rows); 
  } 

public boolean isCellEditable(int row, int column){ 
    if (column ==0){ 
      return false; 
    } 
    else{ 
      return super.isCellEditable(row, column); 
    } 
  } 

} 

Listing 2: 

public class SimpleValueRenderer implements 
TableCellRenderer{ 
  Jpanel cell = new Jpanel(); 
  Jlabel label = new Jlabel(); 

  public SimpleValueRenderer (){ 
    cell.setBackground(Color.darkGray); 
    cell.add(label); 
  } 

public Component getTableCellRendererComponent(JTable 
table, Object value, 
        boolean isSelected, 
        boolean hasFocus, 
        int row, int col){ 
    label.setText(value.toString()); 
    return cell; 
  } 
} 

Listing 3: 

  JTable aTable = new JTable 
.. 
.. 

//previous initialization code... 
  aTable.setDefaultCellRenderer(String.class,new 
  SimpleValueRenderer()); 

Listing 4: 

public class BooleanValueRenderer extends JCheckBox 
implements TableCellRenderer{ 
  public BooleanValueRenderer (){ 
    super(); 
  } 

public Component getTableCellRendererComponent(JTable table, Object value, 
        boolean isSelected, 
        boolean hasFocus, 
        int row, int col){ 
    this.SetValue((Boolean)value); 
    return this; 
  } 
} 

and to use it... 

.. 
.. 

//previous initialization code... 
  aTable.setDefaultCellRenderer(Boolean.class,new 
  BooleanValueRenderer ()); 

Listing 5: 

public void getClassInfo(Object aValue){ 

  Class valueClass = aValue.getClass(); 

//lets write out the name of the class to command line 
  System.out.println(valueClass.getName()); 

//lets get all the methods and then print out their names 
//to command line 
  Method[] classMethods = valueClass.getDeclaredMethods(); 

  for (int i=0; i < classMethods.length;i++){ 
    Method aMethod = classMethods[i]; 
//print out the name 
    System.out.println(aMethod.getName()); 
  } 

} 

Listing 6: 

Class[] args = new Class[2]; 
args[0] = Object.class; 
args[1] = Integer.TYPE; 
//you have to do this because the method takes an int 
//not an Integer 
try{ 
  Method setElementAtMeth = valueClass.getDelaredMethod("setElementAt", args); 
} 
catch (Exception ex){ 
  ex.printStackTrace(); 
} 

Listing 7: 

Object[] vals = new Object[2]; 
vals [0] = new String("Hello there"); 
vals [1] = new Integer(0); 
//Java will correctly convert this to an int 

try{ 
 setElementAtMeth.invoke(aValue, vals); 
//method gets invoked ! 
} 
catch (Exception ex){ 
  ex.printStackTrace(); 
} 

Listing 8: 

public void inspectAButton(Jbutton aBtn){ 
  try{ 
    BeanInfo beanInfo = Introspector.getBeanInfo(aBtn); 
    PropertyDescriptor[] props = beanInfo.getPropertyDescrip     tors(); 
     if (props != null){ 
       for (int i=0; i < props.length; i++){ 
        PropertyDescriptor pd = props[i]; 
        System.out.println("Property " + pd.getDisplayName()        + " has read method: "
		 + pd.getReadMethod().get       Name() + " and write method: " + 
        pd.getWriteMethod().getName()); 
       } 
     } 
  } 
  catch(Exception ex){ 
    ex.printStackTrace(); 
  } 
} 



  

 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.