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
 

Originally I planned to continue with the syntax-highlighting CodeDocument component, but I decided to switch gears and discuss some neat uses for the JTable component that comes with Swing (my apologies go out to all those weeping in the aisles, anxiously awaiting more syntax-highlighting code...oh, just a second, let's dab the tears away before continuing).

One of the cool features of the JTable component is its ability to be customized, right down to the individual table cell. This feature, plus another unspeakably cool feature of Java itself (called reflection, but more on that later), will allow us to build a simple Component Inspector similar to what you'd find in IDEs like Borland's JBuilder or Symantec's VisualCafé.

What Is a Component Inspector?
A Component Inspector is used to look at (and edit) the various properties that make a GUI control, like a text box or a progress bar. The Inspector is activated by selecting a control, then editing the properties through the Inspector. This idea is used not only in Java IDEs, but also in programming tools like Visual Basic and Borland's Delphi (see Figures 1 and 2.).

Figure 1
Figure 1:
Figure 2
Figure 2:

Our Component Inspector will consist of a JFrame, which will house a table with two columns, and a couple of labels above the table, to tell us general information about the component we're looking at. What we should end up with can be seen in Figure 3.

Figure 3
Figure 3:

Now, making a super-sophisticated Component Inspector would be really cool, but the whole point of this article is to learn more about JTables and reflection, so to test this out we'll just have another window that contains a variety of controls, and each time we click on one of them the Component Inspector will be updated. Take a look at Figure 4 to see what this window looks like.

Figure 4
Figure 4:

Tables, Tables...and More Tables
Like the rest of Swing, JTable uses the Model View Controller architecture to allow all sorts of complex customization. One type allows the programmer to specify exactly how each table cell is rendered by the table. A table cell - the individual element at the intersection of a given row and column - can be customized as to how it looks (how it renders itself for the user) and how it's edited. Not only can a JTable be customized at the cell level, but you can also write a simple table model class that can prevent columns from being edited. This is what we want to do since the left-hand column will only need to display values, not edit them. So let's just jump in.

The first thing we're going to look at is the DefaultTableModel class, which inherits from the AbstractTableModel. We could have chosen to subclass the AbstractTableModel, but we would have had to implement everything ourselves. Since the only piece of functionality we want to add at this point is to lock the leftmost column against being edited, it's much simpler to extend an already functional class - namely, DefaultTableModel. To prevent column 0 from being edited, we override the functionality of the method isCellEditable(), which is called whenever the Table Model receives notification that a cell needs to edited. If the function returns true, editing is allowed; if false, then editing is prevented. By returning false at the appropriate times, we can easily prevent column 0 from being edited. Take a look at the code in Listing 1.

The first thing we do is to add all the constructors of the DefaultTableModel so they can be called from our class. Next, we override the isCellEditable() method. In this method we check for what column is being requested. If it's 0, we automatically return false; otherwise we call the super class's isCellEditable method to handle any remaining cases.

Now let's play around a bit and learn how to customize the Table rendering. As I mentioned before, each cell that gets rendered can be customized with its own renderer, which is associated with the class type of the value in the cell. Let's say you had a cell at column 0 and row 0 that was a String object. You could associate a custom cell renderer with any String object, and the table would automatically use your renderer to paint the cell instead of its own. Not only would it paint the cell at column 0, row 0, using your renderer, but any other values in the table that were String objects would also get painted using the same renderer. Any object can be a renderer so long as it implements the TableCellRenderer interface (in the com.sun.java.swing.table package). The TableCellRenderer interface has only one method, getTableCellRendererComponent(), which needs to return a Component object. The arguments of the method can tell you whether the cell is selected or not, whether it has focus, what the value of the cell is, the column and row of the cell, and the actual table component the cell belongs to. A very simple implementation could be the one in Listing 2.

To use this, look at Listing 3.

A cell renderer can also be a component itself. For example, let's say that for boolean objects we want a checkbox to appear (there already is a cell renderer available for boolean values built into the JTable implementation, courtesy of the nice folks at Sun). We could just create a new sub class of JCheckBox and implement the TableCellRenderer interface and we'd be all set to go. Let's look at the example in Listing 4.

One thing to remember is that the value argument of getTableCellRendererComponent() can be null, but you still have to return a component of some sort or the table will throw a sea of exceptions when it tries to draw itself, especially if you scroll down through a number of rows.

Now that we can customize our display of call data, if we were going to allow editing, we'd want to create a cell editor now as well. In this example, however, we're just going to try and display information only.

Mirror, Mirror on the Wall...
Okay, so we know how to display our data, but where do we get it from and how do we get at it? This is accomplished using a very cool - and very powerful - feature of Java called reflection (also sometimes referred to as class or JavaBean introspection). What you read here will be used not only in this article, but again when we come back to our CodeDocument class and start adding features like dropping down a listbox full of the variable's methods and attributes just typed in.

Reflection allows the programmer to ask an object to describe itself by listing all of its methods, fields and method signatures. It is literally like walking up to a person and asking them to describe themselves for you, tell you their family history (who their parents were, where they were born, etc.), list all the things they can do and so on. Like reflection, JavaBeans introspection allows you to ask for all sorts of detailed information pertaining to the specific instance of a bean at runtime, which is what allows tools like JBuilder and VisualCafé to list all the properties of a particular bean.

Reflection starts by obtaining a reference to the object's Class attribute. This is accomplished by the getClass() method in the Object base class. Once you have a Class object, you can get the rest of the information you need - and can even call the methods you retrieve! Let's look at Listing 5 for an example.

The first thing we do in the code is to get the Class object from the argument aValue. Now remember, because we've defined the aValue argument as an object, it could represent anything at runtime - a JFrame, a Hashtable or anything else. Once we have a reference to the class object, we can get the class name through the getName() method, which returns a fully qualified name (i.e., instead of "String", it returns "java.lang.String"). Any of the constructors, methods or fields of the class can be retrieved through calls like getDeclaredMethod() (for a single method) or getDeclaredMethods() (for all of the methods) (for constructors, you would use getDeclaredConstructors(), and so on). Using the getDeclaredMethods() method, we can loop through all the methods and print out their names using the Method class's getName() function. When retrieving a single method, you have to pass in the method name as well as an array of class objects that represent the arguments of the method. So let's say we were going to try and find out if the object had a setElementAt() method. We could do this as shown in Listing 6.

The array of class objects tells Java what types are in the argument list. If there are no arguments, you can just pass in null. Invoking the method is similar to retrieving it. You pass in an array of objects that are the actual values you want passed as arguments to the method. That would be as in Listing 7.

Presto! You have magically invoked a method, determining everything at runtime!

Getting BeanInfo
As I mentioned earlier, if the object you're dealing with is a JavaBean, you can get even more information - things like what kind of property editors it uses, what the display name is, whether a particular property of the JavaBean knows how to paint itself, and many others.

To start, you use the Introspector static class method getBeanInfo(), passing in the Class object of the bean or component you're interested in. The Introspector is nice enough to package everything in a neat interface called BeanInfo that has a number of methods to retrieve things like the icon associated with the bean, as well as all the properties, events and methods for that bean. The item we're really interested in is the list of properties, retrieved through a call to the getPropertyDescriptors() method of the BeanInfo class. This method gives us an array of PropertyDescriptors from which we can get information like the "read" and the "write" methods of the property, the property editor class (if one exists) and other information as well. Another short example of using this is the code in Listing 8.

All this code does is output the available properties with their names, and read and write method names to the command line. If you were interested in finding out what was in any of the properties, you could use the getReadMethod() function and invoke the read method as described earlier.

Tune in next time...
That's it for this month. We'll wrap it up in the next article by combining the two features we discussed: the customization of tables with reflection and JavaBean introspection, and we'll have ourselves a bona fide Component Inspector. In doing this, we'll delve further into the PropertyInspector class, learning how to paint the property and how to display any custom editors that exist for the property. Hope you found this as fascinating as I did writing it! See you next time....

About Bio
Jim Crafton is a staff consultant with Computer Sciences Corporation where he specializes in object-oriented development. He also develops advanced graphics software for Windows and the BeOS. Jim has a Web site at www.one.net/~ddiego/. He can be reached at [email protected]

	

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.