commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Eric Galluzzo <egallu...@einnovation.com>
Subject Re: BasicDynaBean and overriding the toString()
Date Tue, 02 Dec 2003 16:13:47 GMT
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


Mime
View raw message