How to change background color of list rows depending on value

59 views
Skip to first unread message

Dario

unread,
Aug 15, 2014, 4:59:56 AM8/15/14
to codenameone...@googlegroups.com
Hi,
i have a short list of items and i'd like to set the background color of each row basing on the item type.
So i wrote a custom ListCellRenderer, but the final result is that the rows have all the same color, the one of the first item.
Here is my renderer:

public class DevicesRenderer extends Container implements ListCellRenderer {
    private Label nameLabel = new Label("");
    private Label typeLabel = new Label("");
  

    public DevicesRenderer() {
        setLayout(new BorderLayout());
        setCellRenderer(true);

        nameLabel.setUIID("ListTitle");
        typeLabel.setUIID("ListSubtitle");

        Container cnt = new Container(new BoxLayout(BoxLayout.Y_AXIS));
        cnt.addComponent(nameLabel);
        cnt.addComponent(typeLabel);
        addComponent(BorderLayout.WEST, cnt);
    }


    public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) { 
        Device device = (Device) value;

        nameLabel.setText(device.getDescription());
        typeLabel.setText(device.getTypeId());

        int color = device.getColor();

        getUnselectedStyle().setBgColor(color);
        getSelectedStyle().setBgColor(color);
        getPressedStyle().setBgColor(color);
         
        return this;
    }

    public Component getListFocusComponent(List list) {
        return null;
    }

    public void repaint() {
    }
}

Thx for any help.

(Btw, is it ok that the getListCellRendererComponent method is invoked something like 700 times for a list of only 6 items?)

Shai Almog

unread,
Aug 15, 2014, 10:57:08 AM8/15/14
to codenameone...@googlegroups.com, dario....@archeometra.it
Hi,
the renderer is invoked a lot to perform animations etc. so it will be called far more than what you would assume is necessary which is why its a very tricky component to create.
Use List.setMutableRendererBackgrounds(true) to enable this behavior.
We have an optimization in place that draws the background once for all renderers.

dario....@archeometra.it

unread,
Aug 16, 2014, 3:57:23 AM8/16/14
to codenameone...@googlegroups.com, dario....@archeometra.it
Thx Shai, to make it work i applied the flag you suggested and also changed a couple of lines:

int color = device.getColor();
Style style = new Style(0, color, null, (byte)255);
setUnselectedStyle(style);
setSelectedStyle(style);
setPressedStyle(style);

Shai Almog

unread,
Aug 16, 2014, 10:12:43 AM8/16/14
to codenameone...@googlegroups.com, dario....@archeometra.it
I would strongly recommend you don't do that. Replacing the style object is generally not a great idea but doing this in a renderer means you would be allocating dozens of objects per frame slowing down your app considerably.
Use getUnselectedStyle etc. and set the value on those styles.
Reply all
Reply to author
Forward
0 new messages