ListBox and default selection

378 views
Skip to first unread message

Steve C

unread,
Nov 23, 2013, 2:11:53 PM11/23/13
to google-we...@googlegroups.com
In plain old HTML, I can create a select tag, and specify one option as selected, so that it comes up as the selection by default, or gets selected if the user resets the form.

GWT doesn't appear to offer this capability - I don't see anything like setDefaultSelectedIndex(int index), or addItem(String itemText, boolean defaultSelected).  Is this an oversight, or something that was done deliberately?

The situation I'd like to use it for is a set of enumerated column values from a DB, to be listed in alphabetical order, but where the default value isn't the first.  As it stands now, I'm going to have to keep track of the proper index to select when the form is cleared, as opposed to getting it done once when I initially set up the form.

Hmm, that being said, it seems like there's no reset capability anyway - a ResetButton apparently belongs only in a FormPanel, and there's no reset method in ListBox. I suspect that I'm going to have to extend ListBox to provide a way to set a default selected index AND a reset capability. So, this turned into more of a rant than a question, but I'd still be interested in hearing if there was a specific reason those capabilities were left out, or if my anticipated extensions are a bad idea for some reason I haven't thought of.

Jens

unread,
Nov 23, 2013, 3:14:46 PM11/23/13
to google-we...@googlegroups.com
In plain old HTML, I can create a select tag, and specify one option as selected, so that it comes up as the selection by default, or gets selected if the user resets the form.

GWT doesn't appear to offer this capability - I don't see anything like setDefaultSelectedIndex(int index), or addItem(String itemText, boolean defaultSelected).  Is this an oversight, or something that was done deliberately?

Probably an oversight. ListBox also does not support option groups. However you can actually set a default selection when dropping down to the DOM Element layer:

SelectElement select = listBox.getElement().cast();
select.getOptions().getItem(index).setDefaultSelected(true);


Hmm, that being said, it seems like there's no reset capability anyway - a ResetButton apparently belongs only in a FormPanel, and there's no reset method in ListBox.

If you have a FormPanel that contains the ResetButton and your ListBox then the ResetButton should just work. If you don't have a FormPanel then you have to implement the reset logic yourself as the browser won't do it for you automatically.


If you want to see an API for default selection on ListBox then you should file an issue for it (and maybe also provide a patch).

-- J.
Reply all
Reply to author
Forward
0 new messages