Hi Stefano,
If I use both DataRenderer and ItemRenderer I guess - a wild guess - that
the DataRenderer will compute name + " - " + address the first time the
listButton is initialized and then the ItemRenderer will return the value to
show to the user every time he scrolls the list.
I will call (name+" - "+address) as displayName from now, it will computed
each time that your renderer need it (when is visible, mouse events,
highlighted and so on), to avoid this computation all the time you could
keep displayName as variable o final value.
One approach:
public class Customer {
private String key, name, address;
private String displayName;
public Customer(String key, String name, String address) {
this.key = key;
this.name = name;
this.address = address;
}
public String getDisplayName() {
//All the time this conditional is checked(more processor
operations)
if (displayName == null) {
displayName = name + " - " + address;
}
return displayName;
}
}
Second approach:
public class Customer {
private String key, name, address;
private final String displayName;
public Customer(String key, String name, String address) {
this.key = key;
this.name = name;
this.address = address;
//All customers with displayName computed from beginning( more memory usage)
displayName = name + " - " + address;
}
public String getDisplayName() {
return displayName;
}
}
Renderer:
public class CustomerDataRenderer extends ButtonDataRenderer {
@Override
public void render(Object data, Button button, boolean highlighted) {
super.render(data, button, highlighted);
if (data instanceof lbtCustomer) {
Customer customer = (Customer) data;
super.label.setText(customer.getDisplayName());
}
}
}
Both approaches with advantages and disadvantages, remember render() method
is called all the time when data is needed to represent/view.
Regards,
Alejandro
From: Dr. Stefano Sancese [mailto:[email protected]]
Sent: lunes, 02 de agosto de 2010 02:05 p.m.
To: [email protected]
Subject: Re: ListButton - Is it the correct way?
Il 02/08/2010 18:47, Alejandro Vilar ha scritto:
Hi Stefano,
Hi Alejandro,
thanks for your prompt replay.
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:
<snip>
Ok an other subject to learn: renderers
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 I use both DataRenderer and ItemRenderer I guess - a wild guess - that
the DataRenderer will compute name + " - " + address the first time the
listButton is initialized and then the ItemRenderer will return the value to
show to the user every time he scrolls the list.
If you want get the chosen record:
lbtCutomer selectedCustomer =
(lbtCutomer)listButton.getSelectedItem();
Ok, so you don't see any problem associated with the instantiation of 1000
objects.
I'll suggest override equals method in your lbtCustomer class, some list
abilities are based on it.
Ok.
Hope this helps,
Absolutely, thanks again.
Stefano
Alejandro
--
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)
************************************************************************