public interface ListCellRenderer<T>
Known subtypesDefaultListCellRenderer, GenericListCellRenderer
A “rubber stamp” tool that allows us to extract a component (often the same component instance for all invocations) that is initialized to the value of the current item extracted from the model, this component is drawn on the list and discarded. No state of the component is kept and the component is essentially discarded.
This is a very advanced interface it is recommended that you use either the DefaultListCellRenderer or GenericListCellRenderer whenever possible to avoid mistakes.
An instance of a renderer can be developed as such:
class MyYesNoRenderer extends Label implements ListCellRenderer {
Label label = new Label(" ");
public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) {
if( ((Boolean)value).booleanValue() ) {
setText("Yes");
} else {
setText("No");
}
return this;
}
public Component getListFocusComponent(List list) {
return label;
}
}
Form f = new Form("ListRenderer", new BorderLayout());
List lst = new List(Boolean.TRUE, Boolean.FALSE);
f.addComponent(BorderLayout.CENTER, lst);
f.show();
Methods
public abstract Component getListCellRendererComponent(List list, T value, int index, boolean isSelected) | Returns a component instance that is already set to render “value”. |
public abstract Component getListFocusComponent(List list) | Returns a component instance that is painted under the currently focused renderer and is animated to provide smooth scrolling. |
Method details
getListCellRendererComponent
public abstract 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
listList- the list component
valueT- the value to render
indexint- the index in the list
isSelectedboolean- whether the entry is selected
Returns
a component to paint within the list
getListFocusComponent
public abstract 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
listList- the parent list
Returns
a component to use as focus