commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "dumdum 420" <dumdum...@hotmail.com>
Subject Re: BasicDynaBean and overriding the toString()
Date Tue, 02 Dec 2003 05:55:12 GMT
Hi Eric,

This approach works fine and I was now able to override the default behavior 
of the Swing class of overriding toString().

There were minor change to the code tough ..
   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, 
expanded,
           leaf, row, hasFocus );
   }

instead of >> value instanceof DynaBean
it should be DefaultMutableTreeNode and then the reference to the DynaBean 
was fetched by accessing the .getUserObject() method of the 
DefaultMutableTreeNode class.

If you still have a different view point please let me know.

Big thanx for the help.

BP


From: Eric Galluzzo <egalluzzo@einnovation.com>
Reply-To: "Jakarta Commons Users List" <commons-user@jakarta.apache.org>
To: Jakarta Commons Users List <commons-user@jakarta.apache.org>
Subject: Re: BasicDynaBean and overriding the toString()
Date: Mon, 01 Dec 2003 19:30:47 -0500

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, 
expanded,
            leaf, row, hasFocus );
    }
}
--------

Then you could do this:

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

    - Eric



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org

_________________________________________________________________
Gift-shop online from the comfort of home at MSN Shopping!  No crowds, free 
parking.  http://shopping.msn.com


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message