Problem Selecting Listitem in Listbox

asked 2009-12-30 01:04:34 +0800

mahiMahi gravatar image mahiMahi
12 1

updated 2009-12-30 01:06:25 +0800

Hi guys,
I got problem with selecting Listitem..
I'm have an Listbox with datas and checkmark="true",
i have an 5 row, every row i added attribute for the item id while in rendering :

public class EmployeesListModelItemRenderer implements ListitemRenderer,
		Serializable {
        public EmployeesListModelItemRenderer() {
	public void render(Listitem item, Object data) throws Exception {
            item.setAttribute("data", data);

in case..
i double click the row and code execute

    Listitem item = lstbox_emp.getSelectedItem();
    String itemid = item.getAttribute("itemid").toString();
    . . . . . . 

then modal dialog window appear, i edit the 2nd row data(check mark is selected) the dialog.
the 2nd data in is updated.
then i close the dialog,
run this code..
     . . . . . 
    listbox.setSelectedItem(getLastSelectedItem(listbox.getItems(), itemid));
    . . . . 

Method getLastSelectedItem():

11   Listitem getLastSelectedItem(List<Listitem> items, String itemid){
12       for (org.zkoss.zul.Listitem listitem : items) {
13           if (listitem.getAttribute("itemid").equals(itemid)) 
14               return listitem;
15      return new Listitem();
16    }

"getLastSelectedItem()" is use for select the last edited row in listbox.
Problem is when i call "getLastSelectedItem()"
it always make an error NullPointer in line 13...
but No problem when i call "getLastSelectedItem()" if i call that using button event for example.
it seems listitem no been set by "EmployeesListModelItemRenderer" class.
are this because the listitem still in process rendering?
where i should call "getLastSelectedItem()"?


delete flag offensive retag edit

6 Replies

Sort by ยป oldest newest

answered 2009-12-30 03:27:02 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16

updated 2009-12-30 03:28:03 +0800

Hi mahiMahi,

only a hint for selecting a known Object as a ListItem in a Listbox:

myListBox.setModel(new ListModelList(getMyServiceDAO().getAList()));
myListBox.setItemRenderer(new myListModelItemRenderer());

// get the ListModelList back for this Listbox for work with it
ListModelList lml = (ListModelList) myListBox.getModel();

// get Object that we want to select in the Listbox
Branche branche = customer.getBranche();

// select the ListItem in the Listbox by an integerPosition by getting
// it's position in the corresponding ListModelList (lml)
myListBox.setSelectedIndex( lml.indexOf( branche ));


link publish delete flag offensive edit

answered 2009-12-30 05:04:26 +0800

mahiMahi gravatar image mahiMahi
12 1

Hi Sthepan,
That's work!!!.
Nice hint.
So just use lstbox.setSelectedIndex(int index)
by using lstmodel.indexOf(object) for the index
instead of lstbox.setSelectedItem(Listitem item)
by assign and checking attribute value through the item.


link publish delete flag offensive edit

answered 2010-08-18 22:04:32 +0800

joredva gravatar image joredva

Hi Sthepan,

I tried the example but the sentence lml.indexOf(lang) always returns -1
In debug mode I can see that the variable lang is within lml but still not found
What I'm doing wrong
This is the code

lbox_catIdCatalogo.setModel(new ListModelList(getAvanteGTHService().getItemsCatalogo(Sistema.CATALOGO_MASTER,0)));
lbox_catIdCatalogo.setItemRenderer(new CatalogosDropDownListModelItemRenderer());

ListModelList lml = (ListModelList) lbox_catIdCatalogo.getModel();
AvCatalogos lang = getAvanteGTHService().getItemByCatalogoAndIdDato(Sistema.CATALOGO_MASTER, itemCatalogo.getVidcatalogo(),0 );

int ff = lml.indexOf(lang);


Is there another way to Select Listitem in Listbox


link publish delete flag offensive edit

answered 2010-08-18 23:44:00 +0800

TonyQ gravatar image TonyQ

updated 2010-08-18 23:47:05 +0800

first i think you need to check the list from "getAvanteGTHService().getItemsCatalogo(Sistema.CATALOGO_MASTER,0)" contains the lang ?

if there exist , you may check this below

for java docs explain the behavior in list ,

it is talking about List.indexOf will call object.equals to check if the object is equals ,
and the equals method inherits from Object is default to compare with instance.

I guess the problem is , this one instance in list is not same as lang ,
so if you dont overwirte the equals , it will think that is not same object in list. so you got a message for -1 ( item not found )

if you can modifly the AvCatalogos class , you should try to overwrite equals method to make sure it will work correctly.
or just try the simple way , to iterate over collections and compare each object by yourself.

i think there may be some field about "id" or something you can use.

just like


int index = 0 ;
for(int i=0;i<lml.size();++i){
        AvCatalogos  item = (AvCatalogos ) lml.get(i);
       if(item.getId() == lang.getId()){   
               index = il 

// dosomething with index 

link publish delete flag offensive edit

answered 2010-08-19 06:03:35 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16

updated 2010-08-19 06:03:48 +0800

It's exactly like TonyQ said.

Look at your bean class and let Eclipse generate the hashCode and equals method for you.

link publish delete flag offensive edit

answered 2010-08-20 00:09:22 +0800

joredva gravatar image joredva

Thanks Terrytornado and TonyQ

Now work,
I add this code in Avcatalogos class


public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + iiditemcatalogo;

return result;



public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

AvCatalogos other = (AvCatalogos) obj;

if (iiditemcatalogo != other.iiditemcatalogo)

return false;

return true;


link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools




Asked: 2009-12-30 01:04:34 +0800

Seen: 1,654 times

Last updated: Aug 20 '10

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More