java-melwuitlwuit-formlwuit-list

How to style a specific list item in lwuit?


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.


Solution

  • 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.