GWT ListBox not selected by default

I have a GWT ListBox with elements:

listBox = new ListBox();
listBox.addItem("A");
listBox.addItem("B");
listBox.addItem("C");

and I would like it not to be selected so that no item is selected. The initial lack of choice should be symbolized by empty text, and after selecting any item, the user cannot select the "selection item".

Unfortunately, the following line:

listBox.setSelectedIndex(-1);

throws IndexOutOfBoundsException.

Is it possible to get this behavior using the GWT ListBox?

+5
source share
2 answers

Yes, this is a normal situation, because when you call setSelectedIndex(), it checks the index to see if it is in a range. There is a method in the ListBox class.

 private void checkIndex(int index) {
    if (index < 0 || index >= getItemCount()) {
      throw new IndexOutOfBoundsException();
    }
  }

, 0. , :

listBox = new ListBox();
listBox.addItem(" ");
listBox.addItem("A");
listBox.addItem("B");
listBox.addItem("C");
listBox.addChangeHandler(new ChangeHandler() {
        public void onChange(ChangeEvent changeEvent) {
            SelectElement selectElement = listBox.getElement().cast();
            selectElement.getOptions().getItem(0).setDisabled(true);

        }
    });
+8

, ListBox select. select, , , . , - HTML , , . GWT, , ( , addItem, setDefaultSelected (int index)).

0

All Articles