ListBox how set selected item by value

4,892 views
Skip to first unread message

zeroonea

unread,
May 20, 2009, 3:32:22 AM5/20/09
to Google Web Toolkit
when i load data to form to edit, how i set selected item in listbox
by value

umamaheswari sudalaimuthu

unread,
May 20, 2009, 4:15:13 AM5/20/09
to Google-We...@googlegroups.com
you can get the value of the selectitem from listem by
listItm.getselectedItam().getValue();

zeroonea

unread,
May 20, 2009, 5:14:25 AM5/20/09
to Google Web Toolkit
were you read my post?

Thomas Broyer

unread,
May 20, 2009, 8:49:46 AM5/20/09
to Google Web Toolkit

On 20 mai, 09:32, zeroonea <Zeroo...@gmail.com> wrote:
> when i load data to form to edit, how i set selected item in listbox
> by value

Loop over the items until you find the one you want to select, and set
it selected (though depending on whether you have a multiple-selection-
enabled ListBox or not, and/or whether you'd like to clear the current
selection or add to it --in case ismultipleSelect() == true--, you'd
use setItemSelected vs. setSelectedIndex).

GWT could have provided a "shortcut" method (setSelectedValue or
selectByValue) but it would do exactly this, as there's no other way
of doing it at the JS-level (and it would probably result in less-
optimized code, as developers would be using this shortcut method
instead of trying to optimize their own algorithm a bit; denpending on
their use case, multiple selection vs. single selection, replace
selection vs. add to selection, etc.).

zeroonea

unread,
May 20, 2009, 11:35:31 AM5/20/09
to Google Web Toolkit
Okay, i have many listbox in some module, so i decided write a method
to do this loop, share it between several module, a problem is i put
this method in a tool class, but gwt do not allow i import it into
entry point class, so it must be in same package with entry point
class, but i do not want put all entry point classes of other modules
in a same package, so what i should do?

zeroonea

unread,
May 21, 2009, 11:53:55 PM5/21/09
to Google Web Toolkit
bump

Ivano Vingiani

unread,
May 29, 2014, 5:58:48 AM5/29/14
to google-we...@googlegroups.com, Google Web Toolkit
Create a Widget that extends (or wrap) ListBox that implements setValue(value)

Steve C

unread,
May 30, 2014, 12:12:24 PM5/30/14
to google-we...@googlegroups.com, Google-We...@googlegroups.com
One of the first things I ever put into my personal utility classes module was an extension of ListBox to have setValue(String) and getValue(String) methods.

Or, you could use ValueListBox instead of ListBox, but it's kind of a pain to work with due to the need for a Renderer.  (But, it does allow the use of object values, not just Strings).
Message has been deleted

Dhinakar

unread,
Sep 25, 2017, 1:26:58 PM9/25/17
to GWT Users
private void setSelectedValue(ListBox lBox, String str) {
String text = str;
int indexToFind = -1;
for (int i = 0; i < lBox.getItemCount(); i++) {
//Match by Value
if (lBox.getValue(i).equals(text)) {
indexToFind = i;
break;
}
//Match by Text 
                       if (lBox.getItemText(i).equals(text)) {
indexToFind = i;
break;
}
}
lBox.setSelectedIndex(indexToFind);
Reply all
Reply to author
Forward
0 new messages