views:

1013

answers:

2

Hey there Stackoverflow,

I got a doubt regarding pre-selecting(setSelectedIndex(index)) an item in a ListBox, Im using Spring + GWT.

I got a dialog that contains a painel, this panel has a flexpanel, in which I've put a couple ListBox, this are filled up with data from my database.

But this painel is for updates of an entity in my database, thus I wanted it to pre-select the current properties for this items, alowing the user to change at will.

I do the filling up in the update method of the widget.

I tried setting the selectedItem in the update method, but it gives me an null error.

I've searched a few places and it seems that the listbox are only filled at the exact moment of the display. Thus pre-selecting would be impossible.

I thought about some event, that is fired when the page is displayed.

onLoad() doesnt work..

Anyone have something to help me out in here?

Thx in advance,

Rodrigo Dellacqua

+1  A: 

I really think you can set the selection before it's attached and displayed, but you have to have added the data before you can select an index. If this is a single select box you could write something like this:

void updateListContent(MyDataObject selected, List<MyDataObject> list){
     for(MyDataObject anObject : list){
          theListBox.addItem(anObject.getTextToDisplay(), anObjec.getKeyValueForList());
     }
     theListBox.setSelectedIndex(list.indexOf(selected));
}

if this is a multiple select box something like this may work:

void updateListContent(List<MyDataObject> allSelected, List<MyDataObject> list){
     for(MyDataObject anObject : list){
          theMultipleListBox.addItem(anObject.getTextToDisplay(), anObjec.getKeyValueForList());
     }
     for(MyDataObject selected : allSelected){
         theMultipleListBox.setItemSelected(list.indexOf(selected), true);
     }
}

(Note I haven't actually compiled this, so there might be typos. And this assumes that the selected element(s) is really present in the list of possible values, so if you cant be sure of this you'll need to add some bounds checking.)

Stein G. Strindhaug
A: 

I've been happily setting both the values and the selection index prior to attachment so as far as I'm aware it should work. There's a bug however when setting the selected index to -1 on IE, see http://code.google.com/p/google-web-toolkit/issues/detail?id=2689.

Andrew