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 16:17:45 GMT

I know this is not the place but can u share you custom TreeModel and how 
are u doing your job with that using the DynaBean. (it will be fun to know 
:))

BTW: Thanx a lot for help.

Bhanu

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: Tue, 02 Dec 2003 11:13:47 -0500

dumdum 420 wrote:

>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.

I see.  Sorry, I didn't realize you were using a DefaultTreeModel.  I tend 
to always implement TreeModel myself, so that the value objects are actually 
my business objects; but your approach works fine too.  In that case, the 
code should probably look like this:

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 DefaultMutableTreeNode )
    {
        Object userObject = ( (DefaultMutableTreeNode) value 
).getUserObject();
        if ( userObject instanceof DynaBean )
        {
            Object property = ( (DynaBean) userObject ).get( 
getPropertyName() );
        }
    }
    return super.getTreeCellRendererComponent( tree, property, sel, 
expanded,
        leaf, row, hasFocus );
}

That way, you're insulated against any ClassCastExceptions.  Sometimes it's 
good to throw a ClassCastException; but I'd say that in a renderer, you 
don't want to, because you get nasty visual artifacts and piles of stack 
traces printed out by the AWT event queue.

    - Eric



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

_________________________________________________________________
online games and music with a high-speed Internet connection!  Prices start 
at less than $1 a day average.  https://broadband.msn.com (Prices may vary 
by service area.)


---------------------------------------------------------------------
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