Skip to main content

Concrete List sample

1 reply [Last post]
Anonymous

Hi !

I was wondering if the lwuit devs would be kind enough to provide us with a
concrete example of a custom list object.
I'm talking about a concrete example of a List with a custom ListModel and
ListCellRenderer that would take a custom object (for example a Message or a
User ) or something more elaborate than a String , to display information
in a list.
I'm trying to understand how all this works and even if i'm sure I'm pretty
close to unlocking the List's secrets I'd feel more confident if I had
something to base myself upon before going any further.

I've read the tutorial and developer docs and I'm maybe missing something
but I don't really understand a couple of things :

What is *getListFocusComponent* (List list) supposed to do ?

Who calls getListCellRendererComponent and *getListFocusComponent ? *

and Here is my implementation of getListCellRendererComponent. can you guys
tell me if it looks correct ?

public Component getListCellRendererComponent(List list, Object value , int
index, boolean isSelected) {

if(!(value instanceof Message)){
throw new IllegalArgumentException("The Object passed in
parameter was not an instance of Message. We can't render the Message
correctly. It was an instance of"+ value.getClass()); //it's always a string
... why ?
}

Container displayedMessageContainer = new Container();
displayedMessageContainer.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
Message msg = (Message) value;

Label bodyLabel = new Label(msg.getBody());

String msgInfo = "";
if(GeneralUtils.isToday(msg.getDate().longValue())){
msgInfo += "Sent today at ";
msgInfo +=
GeneralUtils.longToTimeString(msg.getDate().longValue());

}else{
msgInfo += "Sent ";
msgInfo +=
GeneralUtils.longToDateString(msg.getDate().longValue());
}

if(msg.getOriginatorJID().equals(localUserJID)){
msgInfo += " by me.";
}else{
msgInfo += " by " + msg.getOriginatorJID();
}
Label infoLabel = new Label(msgInfo);
//todo display the avatar

displayedMessageContainer.addComponent(infoLabel);
displayedMessageContainer.addComponent(bodyLabel);

return displayedMessageContainer;

}

Thanks a lot :)

Cheers,

Alex !
[att1.html]

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Francis De Brabandere

The components in LWUIT are mostly based on those available in Swing.
You might want to have a look at this tutorial:
http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-JList.html

just replace JList with List and JLabel with Label and ...

On Sat, Jun 28, 2008 at 11:38 PM, az lists wrote:
> Hi !
>
> I was wondering if the lwuit devs would be kind enough to provide us with a
> concrete example of a custom list object.
> I'm talking about a concrete example of a List with a custom ListModel and
> ListCellRenderer that would take a custom object (for example a Message or a
> User ) or something more elaborate than a String , to display information
> in a list.
> I'm trying to understand how all this works and even if i'm sure I'm pretty
> close to unlocking the List's secrets I'd feel more confident if I had
> something to base myself upon before going any further.
>
> I've read the tutorial and developer docs and I'm maybe missing something
> but I don't really understand a couple of things :
>
> What is getListFocusComponent (List list) supposed to do ?
>
> Who calls getListCellRendererComponent and getListFocusComponent ?
>
> and Here is my implementation of getListCellRendererComponent. can you guys
> tell me if it looks correct ?
>
>
> public Component getListCellRendererComponent(List list, Object value , int
> index, boolean isSelected) {
>
> if(!(value instanceof Message)){
> throw new IllegalArgumentException("The Object passed in
> parameter was not an instance of Message. We can't render the Message
> correctly. It was an instance of"+ value.getClass()); //it's always a string
> ... why ?
> }
>
> Container displayedMessageContainer = new Container();
> displayedMessageContainer.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
> Message msg = (Message) value;
>
> Label bodyLabel = new Label(msg.getBody());
>
>
> String msgInfo = "";
> if(GeneralUtils.isToday(msg.getDate().longValue())){
> msgInfo += "Sent today at ";
> msgInfo +=
> GeneralUtils.longToTimeString(msg.getDate().longValue());
>
> }else{
> msgInfo += "Sent ";
> msgInfo +=
> GeneralUtils.longToDateString(msg.getDate().longValue());
> }
>
> if(msg.getOriginatorJID().equals(localUserJID)){
> msgInfo += " by me.";
> }else{
> msgInfo += " by " + msg.getOriginatorJID();
> }
> Label infoLabel = new Label(msgInfo);
> //todo display the avatar
>
>
> displayedMessageContainer.addComponent(infoLabel);
> displayedMessageContainer.addComponent(bodyLabel);
>
> return displayedMessageContainer;
>
> }
>
>
>
> Thanks a lot :)
>
> Cheers,
>
> Alex !
>

--
http://www.somatik.be
Microsoft gives you windows, Linux gives you the whole house.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
For additional commands, e-mail: users-help@lwuit.dev.java.net