How to style a specific list item in lwuit?

541 views Asked by At

I am need of a requirement that in a list, some of the list item should exhibit different style than others. How can this be achieved in lwuit?

For Example,

        List menu = new List();
        menu.addItem("1. Green");
        menu.addItem("2. Red");
        menu.addItem("3. Blue");

In this list Each item should have the style of representing its color(i.e) Green should have green Background and Red should have Red Background. Is it possible in LWUIT? How can we achieve this?

Thanks in Advance.

2

There are 2 answers

1
Shai Almog On BEST ANSWER

You must create a cell renderer for this use case. Just derive 'DefaultListCellRenderer' e.g.:

DefaultListCellRenderer rend = new DefaultListCellRenderer() {
     public Component getCellRendererComponent(Component list, Object model, Object value, int index, boolean isSelected) {
           Component c = super.getCellRendererComponent(...);
           c.getStyle().setBgTransparency(255);
           c.getStyle().setBgColor(theColorYouWant);
           return c;
     }
};

Then set this renderer to the list. You will probably need some additional refinements here since this is a WAY oversimplified example of a renderer.

1
Ravi Shenoy On

This is one way of doing it. 1. Create a component for each item in the list 2. Add the bg color and text to it. 3. Once done, add it to a form or any other custon component you have created.

Other way: You can create your own List renderer. Here is some information on how you can do it