commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Eric Galluzzo <>
Subject Re: BasicDynaBean and overriding the toString()
Date Tue, 02 Dec 2003 00:30:47 GMT
dumdum 420 wrote:

> Hi All,
> I am using the BasicDynaBean for my entire project which seemed to be 
> working fine till I started coding for the frontend which is java 
> swing based.
> Since the BasicDynaBean has not overridden the toString method when I 
> try to display my JTree using these beans all i get is the Objects 
> memory address which I am not really interested.
> Now since I have lot of swing based componenets  which have similar 
> behaviour I am wondering how can I override toString method to get 
> desired results ie may be return a propertyName say for example

Instead of doing this, why not define a TreeCellRenderer for your 
JTree?  Then, instead of putting view-related information into your 
model, you can define the display of your beans in your view.  This is 
particularly important if you are displaying the same objects multiple 
times, in different ways, throughout your application.  You could define 
a class something like this (this is coded off the top of my head, it 
may not compile):

package example;

import java.awt.Component;

import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;

import org.apache.beanutils.DynaBean;

public class DynaBeanTreeCellRenderer extends DefaultTreeCellRenderer
    protected String fPropertyName;

    public DynaBeanTreeCellRenderer( String propertyName )
        fPropertyName = propertyName;

    public String getPropertyName()
        return fPropertyName;

    public void setPropertyName( String propertyName )
        fPropertyName = propertyName;

    public Component getTreeCellRendererComponent( JTree tree, Object value,
       boolean sel, boolean expanded, boolean leaf, int row, boolean 
hasFocus )
        Object property = value;
        if ( value != null && value instanceof DynaBean )
            Object property = ( (DynaBean) value ).get( getPropertyName() );
        return super.getTreeCellRendererComponent( tree, property, sel, 
            leaf, row, hasFocus );

Then you could do this:

JTree tree = new JTree();
tree.setCellRenderer( new DynaBeanTreeCellRenderer( "person_Name" );

    - Eric

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message