List with checkbox using LWUIT

2k Views Asked by At

I am using LWUIT for getting a search facility for selection in the List. Now I want to know how can I display the list with CheckBoxes?

list=new List(vector);
cform.addComponent(list);
cform.addComponent(t);
cform.show();
1

There are 1 best solutions below

1
On

I don't know if there is a more simple solution then mine, but mine is highly customizable and can serve for a lot of purposes.

List l = new List;

Vector v = new Vector();
for(int i = 0; i < 10; ++i){
   v.addElement(new CheckItem("itemtekst"));
}

l.setListCellRenderer(new CheckItemRenderer());
l.setModel(new CheckItemModel(v));

the code above makes it work. As you can guess you have to make a new class and override two to make it work.

CHECKITEM: this class has a string and an image. as well as setters and getters. it also has a boolean that shows if it is checked or not.

CHECKITEMRENDERER: has a label for the string and the image of the checkitem it extends Container and implements ListCellRenderer

CHECKITEMMODEL: this extends the defaultlistmodel. it has methods to get the checkeditems and setthem checked or unchecked.

to recap:

  • set the correct items in the vector
  • set the correct renderer
  • set the correct model

and to use it add an actionlistener or it will al be for nothing.