public class GenericListCellRenderer<T>

  1. Object
  2. GenericListCellRenderer

ImplementsCellRenderer<T>, ListCellRenderer<T>

The generic list cell renderer can display containers or arbitrary Codename One components as items in a list, notice that we strongly discourage usage of lists.. It relies on the source data being a Map object. It extracts values from the Map using the component name as an indication to the Map key lookup.

This renderer supports label tickering, check boxes/radio buttons etc. seamlessly.

Please notice that you must use at least two distinct instances of the component when passing them to the constructor, reusing the same instance WILL NOT WORK!

Furthermore, the renderer instance cannot be reused for multiple lists, each list will need a new instance of this renderer!

Sample usage for this renderer follows:

public void showForm() {
    com.codename1.ui.List list = new com.codename1.ui.List(createGenericListCellRendererModelData());
    list.setRenderer(new GenericListCellRenderer(createGenericRendererContainer(), createGenericRendererContainer()));
    Form hi = new Form("GenericListCellRenderer", new BorderLayout());
    hi.add(BorderLayout.CENTER, list);
    hi.show();
}

private Container createGenericRendererContainer() {
    Label name = new Label();
    name.setFocusable(true);
    name.setName("Name");
    Label surname = new Label();
    surname.setFocusable(true);
    surname.setName("Surname");
    CheckBox selected = new CheckBox();
    selected.setName("Selected");
    selected.setFocusable(true);
    Container c = BorderLayout.center(name).
            add(BorderLayout.SOUTH, surname).
            add(BorderLayout.WEST, selected);
    c.setUIID("ListRenderer");
    return c;
}

private Object[] createGenericListCellRendererModelData() {
    Map[] data = new HashMap[5];
    data[0] = new HashMap<>();
    data[0].put("Name", "Shai");
    data[0].put("Surname", "Almog");
    data[0].put("Selected", Boolean.TRUE);
    data[1] = new HashMap<>();
    data[1].put("Name", "Chen");
    data[1].put("Surname", "Fishbein");
    data[1].put("Selected", Boolean.TRUE);
    data[2] = new HashMap<>();
    data[2].put("Name", "Ofir");
    data[2].put("Surname", "Leitner");
    data[3] = new HashMap<>();
    data[3].put("Name", "Yaniv");
    data[3].put("Surname", "Vakarat");
    data[4] = new HashMap<>();
    data[4].put("Name", "Meirav");
    data[4].put("Surname", "Nachmanovitch");
    return data;
}
public void showForm() {
    com.codename1.ui.List list = new com.codename1.ui.List(createGenericListCellRendererModelData());
    list.setRenderer(new GenericListCellRenderer(createGenericRendererContainer(), createGenericRendererContainer()));
    Form hi = new Form("GenericListCellRenderer", new BorderLayout());
    hi.add(BorderLayout.CENTER, list);
    hi.show();
}

private Container createGenericRendererContainer() {
    Label name = new Label();
    name.setFocusable(true);
    name.setName("Name");
    Label surname = new Label();
    surname.setFocusable(true);
    surname.setName("Surname");
    CheckBox selected = new CheckBox();
    selected.setName("Selected");
    selected.setFocusable(true);
    Container c = BorderLayout.center(name).
            add(BorderLayout.SOUTH, surname).
            add(BorderLayout.WEST, selected);
    c.setUIID("ListRenderer");
    return c;
}

private Object[] createGenericListCellRendererModelData() {
    Map[] data = new HashMap[5];
    data[0] = new HashMap<>();
    data[0].put("Name", "Shai");
    data[0].put("Surname", "Almog");
    data[0].put("Selected", Boolean.TRUE);
    data[1] = new HashMap<>();
    data[1].put("Name", "Chen");
    data[1].put("Surname", "Fishbein");
    data[1].put("Selected", Boolean.TRUE);
    data[2] = new HashMap<>();
    data[2].put("Name", "Ofir");
    data[2].put("Surname", "Leitner");
    data[3] = new HashMap<>();
    data[3].put("Name", "Yaniv");
    data[3].put("Surname", "Vakarat");
    data[4] = new HashMap<>();
    data[4].put("Name", "Meirav");
    data[4].put("Surname", "Nachmanovitch");
    return data;
}

Fields

public static final String ENABLED = "$$ENABLED$$"If this flag exists in a Map of data the renderer will enable/disable the entries, the flag assumes either Boolean.TRUE or Boolean.FALSE.
public static final String SELECT_ALL_FLAG = "$$SELECTALL$$"Put this flag as a Map key to indicate that a checkbox entry rendered by this renderer should act as a “select all” entry and toggle all other entries.

Constructors

public GenericListCellRenderer(Component selected, Component unselected)Constructs a generic renderer with the given selected/unselected components
public GenericListCellRenderer(Component odd, Component oddUnselected, Component even, Component evenUnselected)Constructs a generic renderer with the given selected/unselected components for odd/even values allowing a “pinstripe” effect

Methods

public static URLImage.ImageAdapter getDefaultAdapter()The default adapter to use for image URLs
public static void setDefaultAdapter(URLImage.ImageAdapter aDefaultAdapter)The default adapter to use for image URLs
public void updateIconPlaceholders()Updates the placeholder instances, this is useful for changing the URLImage placeholder in runtime as might happen in the designer
public Button extractLastClickedComponent()Allows partitioning the renderer into “areas” that can be clicked.
public Component getCellRendererComponent(Component list, Object model, T value, int index, boolean isSelected)Returns a component instance that is already set to render “value”.
public Component getListCellRendererComponent(List list, T value, int index, boolean isSelected)Returns a component instance that is already set to render “value”.
public Component getListFocusComponent(List list)Returns a component instance that is painted under the currently focused renderer and is animated to provide smooth scrolling.
public Component getFocusComponent(Component list)Returns a component instance that is painted under the currently focused renderer and is animated to provide smooth scrolling.
public boolean isSelectionListener()
public void setSelectionListener(boolean selectionListener)
public Component getSelected()
public Component getUnselected()
public Component getSelectedEven()
public Component getUnselectedEven()
public boolean isFisheye()In fisheye rendering mode the renderer maintains selected component drawing
public void setFisheye(boolean fisheye)In fisheye rendering mode the renderer maintains selected component drawing
public URLImage.ImageAdapter getAdapter()The adapter used when dealing with image URL’s
public void setAdapter(URLImage.ImageAdapter adapter)The adapter used when dealing with image URL’s

Inherited methods

Field details

ENABLED

public static final String ENABLED = "$$ENABLED$$"
If this flag exists in a Map of data the renderer will enable/disable the entries, the flag assumes either Boolean.TRUE or Boolean.FALSE. Notice that just setting it to false when necessary will not work, when its used it must be applied to all entries otherwise the reuse of the renderer component will break this feature.

SELECT_ALL_FLAG

public static final String SELECT_ALL_FLAG = "$$SELECTALL$$"
Put this flag as a Map key to indicate that a checkbox entry rendered by this renderer should act as a “select all” entry and toggle all other entries. The value for this entry is ignored

Constructor details

GenericListCellRenderer

public GenericListCellRenderer(Component selected, Component unselected)
Constructs a generic renderer with the given selected/unselected components

Parameters

selected Component
indicates the selected value for the renderer
unselected Component
indicates the unselected value for the renderer

GenericListCellRenderer

public GenericListCellRenderer(Component odd, Component oddUnselected, Component even, Component evenUnselected)
Constructs a generic renderer with the given selected/unselected components for odd/even values allowing a “pinstripe” effect

Parameters

odd Component
indicates the selected value for the renderer
oddUnselected Component
indicates the unselected value for the renderer
even Component
indicates the selected value for the renderer
evenUnselected Component
indicates the unselected value for the renderer

Method details

getDefaultAdapter

public static URLImage.ImageAdapter getDefaultAdapter()
The default adapter to use for image URLs

Returns

the defaultAdapter

setDefaultAdapter

public static void setDefaultAdapter(URLImage.ImageAdapter aDefaultAdapter)
The default adapter to use for image URLs

Parameters

aDefaultAdapter URLImage.ImageAdapter
the defaultAdapter to set

updateIconPlaceholders

public void updateIconPlaceholders()
Updates the placeholder instances, this is useful for changing the URLImage placeholder in runtime as might happen in the designer

extractLastClickedComponent

public Button extractLastClickedComponent()
Allows partitioning the renderer into “areas” that can be clicked. When receiving an action event in the list this method allows a developer to query the renderer to “see” whether a button within the component was “touched” by the user on a touch screen device. This method will reset the value to null after returning a none-null value!

Returns

a button or null

getCellRendererComponent

public Component getCellRendererComponent(Component list, Object model, T value, int index, boolean isSelected)
Returns a component instance that is already set to render “value”. While it is not a requirement many renderes often derive from a component (such as a label) and return “this”. Notice that a null value for the value argument might be sent when refreshing the theme of the list.

Parameters

list Component
the list component
model Object
the model behind the render
value T
the value to render
index int
the index in the list
isSelected boolean
whether the entry is selected

Returns

a component to paint within the list

getListCellRendererComponent

public Component getListCellRendererComponent(List list, T value, int index, boolean isSelected)
Returns a component instance that is already set to render “value”. While it is not a requirement many renderes often derive from a component (such as a label) and return “this”. Notice that a null value for the value argument might be sent when refreshing the theme of the list.

Parameters

list List
the list component
value T
the value to render
index int
the index in the list
isSelected boolean
whether the entry is selected

Returns

a component to paint within the list

getListFocusComponent

public Component getListFocusComponent(List list)
Returns a component instance that is painted under the currently focused renderer and is animated to provide smooth scrolling. When the selection moves, this component is drawn above/below the list items - it is recommended to give this component some level of transparency (see above code example). This method is optional, an implementation can choose to return null.

Parameters

list List
the parent list

Returns

a component to use as focus

getFocusComponent

public Component getFocusComponent(Component list)
Returns a component instance that is painted under the currently focused renderer and is animated to provide smooth scrolling. When the selection moves, this component is drawn above/below the list items - it is recommended to give this component some level of transparency (see above code example). This method is optional an implementation can choose to return null.

Parameters

list Component
the parent list

Returns

a component to use as focus

isSelectionListener

public boolean isSelectionListener()

Returns

the selectionListener

setSelectionListener

public void setSelectionListener(boolean selectionListener)

Parameters

selectionListener boolean
the selectionListener to set

getSelected

public Component getSelected()

Returns

the selected

getUnselected

public Component getUnselected()

Returns

the unselected

getSelectedEven

public Component getSelectedEven()

Returns

the selectedEven

getUnselectedEven

public Component getUnselectedEven()

Returns

the unselectedEven

isFisheye

public boolean isFisheye()
In fisheye rendering mode the renderer maintains selected component drawing

Returns

the fisheye

setFisheye

public void setFisheye(boolean fisheye)
In fisheye rendering mode the renderer maintains selected component drawing

Parameters

fisheye boolean
the fisheye to set

getAdapter

public URLImage.ImageAdapter getAdapter()
The adapter used when dealing with image URL’s

Returns

the adapter

setAdapter

public void setAdapter(URLImage.ImageAdapter adapter)
The adapter used when dealing with image URL’s

Parameters

adapter URLImage.ImageAdapter
the adapter to set