pivot-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Alejandro Vilar" <alejandro.vi...@synacom.com.bo>
Subject RE: ListButton - Is it the correct way?
Date Mon, 02 Aug 2010 16:47:28 GMT
Hi Stefano,

 

Your approach works well because "toString" method  only be called when a
customer is painted, but it doesn't avoids some possible side effects in the
rest of your code(i.e. logging). Another way is to use renderers as follows:

 

import org.apache.pivot.wtk.Button;

import org.apache.pivot.wtk.content.ButtonDataRenderer;

 

public class CustomerDataRenderer extends ButtonDataRenderer {

@Override

public void render(Object data, Button button, boolean highlighted) {

super.render(data, button, highlighted);

if (data instanceof lbtCustomer) {

lbtCustomer customer = (lbtCustomer) data;

super.label.setText(customer.getName() + "-" + customer.getAddress());

}

}

}

 

Also you can keep the computed name inside a variable in your lbtCustomer
instance to compute it  just once. To setup this renderer in your
ListButton:

 

 

ListButton listButton = new ListButton(customers);

listButton.setDataRenderer(new CustomerDataRenderer());

listButton.setItemRenderer(new CustomerItemRenderer());  //<-- same as data
renderer, but extending from ListViewItemRenderer

 

If you want get the chosen record:

 

                lbtCutomer selectedCustomer =
(lbtCutomer)listButton.getSelectedItem();

 

I'll suggest override equals method in your lbtCustomer class, some list
abilities are based on it.

 

Hope this helps,

 

Alejandro

 

 

From: Dr. Stefano Sancese [mailto:stefano@sancese.com] 
Sent: lunes, 02 de agosto de 2010 11:20 a.m.
To: user@pivot.apache.org
Subject: ListButton - Is it the correct way?

 

Hi to all,

in a form, I have a ListButton that I need to populate with 1000 customers
data (Id, Name, Address).

I wrote this class:

    class lbtCustomer {
            private String id;
            private String name;
            private String address;

            lbtCustomer(String c1, String c2, String c3) {
                id = c1;
                name = c2;
                address = c3;
            }

            public String getKey() {
                return id;
            }

            @Override
            public String toString() {
               return name + " - " + address;
            }
        }

and I populated the ArrayList of the ListButton with:

    ArrayList lbtValues = new ArrayList();

    lbtCustomer r1 = new lbtCustomer("1","John Doe","New York");;
    lbtCustomer r2 = new lbtCustomer("8","Charlie Brown","Los Angeles");
    lbtCustomer r3 = new lbtCustomer("2","Donald Duck","Orlando");
    lbtCustomer r4 = new lbtCustomer("9","Snoopy","Los Angeles");

    lbtValues.add(r1);
    lbtValues.add(r2);
    lbtValues.add(r3);
    lbtValues.add(r4);

    listButtonTest.setListData(lbtValues);

With the toString method I can format the information showed to the user and
with the getKey method I can retrieve the Id of the chosen record.

The test case works, but I wonder if there is a better way.

I'm concerned about the cost (CPU and memory) associated with the
instantiation of the 1000 objects that I need for the real case.

Perhaps two array lists: one with the data to show to the user and the other
to store the id of the corresponding Customer?


Ciao 

Stefano


P.s. I'm realy green to java and - YES - I'm Reading the F...... Manuals.
There are simply too many of them ;-)



-- 
Dr. Stefano Sancese
 
WatchGuard Certified System Professional - http://www.watchguard.com
Socio Clusit - Associazione Italiana per la Sicurezza Informatica
 
************************************************************************
 
In God we trust, all others we monitor (National Security Agency)
 
************************************************************************

Mime
View raw message