You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by dumdum 420 <du...@hotmail.com> on 2003/12/01 20:18:53 UTC

BasicDynaBean and overriding the toString()

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

public String toString(){
     return this.get("person_Name");
}

BTW: Remember that the Bean is same across the project and I cannot do this 
method in the bean since then for say another usecase this property does not 
even exist.

If I am not clear please ask me again.

I am really stuck and really need a solution.

Thanx in advace.

dumdum420

_________________________________________________________________
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


Re: BasicDynaBean and overriding the toString()

Posted by robert burrell donkin <ro...@blueyonder.co.uk>.
if you really want a good string method, why not simply create a 
BasicDynaBean subclass with such a method?

(maybe i'm missing something but) why not just store the property name 
in a field when can be set when the dynabean is created?

- robert

On 1 Dec 2003, at 19:18, 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
>
> public String toString(){
>     return this.get("person_Name");
> }
>
> BTW: Remember that the Bean is same across the project and I cannot do 
> this method in the bean since then for say another usecase this 
> property does not even exist.
>
> If I am not clear please ask me again.
>
> I am really stuck and really need a solution.
>
> Thanx in advace.
>
> dumdum420
>
> _________________________________________________________________
> 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
>


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


Re: BasicDynaBean and overriding the toString()

Posted by Eric Galluzzo <eg...@einnovation.com>.
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


Re: BasicDynaBean and overriding the toString()

Posted by __matthewHawthorne <ma...@phreaker.net>.
It sounds like you need to define some type of display wrapper.


You could create a static method to do this:

static final String display(BasicDynaBean bean, String prop) {
	return return bean.get(prop);
}


Or create a hierarchy -- if you're into this sort of thing...

public abstract BasicDynaBeanView
	BasicDynaBeanView(BasicDynaBean bean) {

	abstract String display() {}
}

So, if I'm understanding you correctly, there are tons of ways to solve 
your problem.  You need to create a layer in between your GUI and the 
DynaBeans which contain the data.




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
> 
> public String toString(){
>     return this.get("person_Name");
> }
> 
> BTW: Remember that the Bean is same across the project and I cannot do 
> this method in the bean since then for say another usecase this property 
> does not even exist.
> 
> If I am not clear please ask me again.
> 
> I am really stuck and really need a solution.
> 
> Thanx in advace.
> 
> dumdum420
> 
> _________________________________________________________________
> 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


Re: BasicDynaBean and overriding the toString()

Posted by John Zoetebier <jo...@transparent.co.nz>.
Have you ever considered using a HashMap wrapper ?
The wrapper has a HashMap with {FieldName, FieldValue}

Two simple methods do the job:
public Object getField(String fieldName) {
	return fieldMap.get(fieldName);
}

public void setField(String fieldName, Object fieldValue) {
	this.fieldMap.set(fieldName, fieldValue);
}

You can enhance the wrapper with meta information of for example:
- the field names (or column names if you like).
- field type

I call the wrapper DataModel and can use it in Web and GUI systems.

This solution is:
- simple as you do not have to write a dedicated Bean for each entity in 
your system.
- you need only 1 wrapper for all entities in your system
- fast as you do not have to set or get zillions of getters and setters.
- no dependencies between data store and data model
	The problem with JavaBeans for storing the data model is that any change 
in the data store requires a change in the software.
- saves development time, easy to maintain, easy to test and debug.
	On a big project this wrapper can save hundreds if not thousands of hours 
work.

I simply felt that the standard solution of Jakarta Struts using Java 
Beans is inadequate as it introduces hard coupling of data store and data 
model classes. I have seen some cases in which essentially the same date 
structure was replicated in many layers. One additional field means going 
through many classes manually.
The hashMap wrapper solves this problem.
Lately I saw a similar solution online in a magazine lately, but 
unfortunately cannot find the URL. They called it something like 
DataJewel.java.

-- 
John Zoetebier
Web site: http://www.transparent.co.nz



On Mon, 01 Dec 2003 19:18:53 +0000, dumdum 420 <du...@hotmail.com> 
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
>
> public String toString(){
>      return this.get("person_Name");
> }
>
> BTW: Remember that the Bean is same across the project and I cannot do 
> this method in the bean since then for say another usecase this property 
> does not even exist.
>
> If I am not clear please ask me again.
>
> I am really stuck and really need a solution.
>
> Thanx in advace.
>
> dumdum420
>
> _________________________________________________________________
> 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
>
>



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