Why @UiFactory ListBox makeListBox1() method influences to all UiBinder ListBoxes (ListBox1, ListBox2, ListBox3 etc)?

StackOverflow https://stackoverflow.com/questions/21953579

  •  15-10-2022
  •  | 
  •  

Question

Ok, I have a need to use many ListBoxes in UiBinder.

Ok, in View.ui.xml file:

<g:ListBox ui:field='listBox1' visibleItemCount='3' >
    <g:item value='1'> Car </g:item>
    <g:item value='2'> Car2 </g:item>
    <g:item value='3'> Car3 </g:item>
    //more item
</g:ListBox>

<g:ListBox ui:field='listBox2' visibleItemCount='3' >
    <g:item value='1'> Bike </g:item>
    <g:item value='2'> Bike2 </g:item>
    <g:item value='3'> Bike3 </g:item>
    //more item
</g:ListBox>

// more ui binder ListBox here

Now I want to setMultipleSelect for some ListBoxes only, so I can do something like this <g:ListBox ui:field='listBox2' visibleItemCount='3' multipleSelect="true" >, it works fine but setMultipleSelect(boolean multiple) was deprecated, Google said:

@Deprecated public void setMultipleSelect(boolean multiple)

Deprecated. use ListBox(boolean) instead Sets whether this list allows multiple selections. NOTE: The preferred way of enabling multiple selections in a list box is by using the ListBox(boolean) constructor. Using this method can spuriously fail on Internet Explorer 6.0.

So it means we don't use setMultipleSelect but use constructor ListBox(boolean) to set the MultipleSelection, so here is what I did in View.java

@UiField ListBox listBox1;
@UiField ListBox listBox2;
@UiFactory ListBox makeListBox1(){
    listBox1=new ListBox(true);
    return listBox1;
}

However, the above code apply ListBox(true) for all the ListBoxes (listBox1, listBox2, etc). I don't want all ListBoxes have MultipleSelection cos some other ListBoxes need to have single selection only.

So why @UiFactory ListBox makeListBox1() method influences to all ListBoxes & how to fix it?

Not sure provided=true can help?

Was it helpful?

Solution

Try provided=true:

@UiField(provided = true) ListBox listBox1;

public MyClass() {
      listBox1=new ListBox(true);
      initWidget(uiBinder.createAndBindUi(this));
}

It must solve your problem

OTHER TIPS

The name of the method isn't taken into account, only the return type, and it's therefore used for all widgets in the current template with that type. If you want specific instances for some fields, then as Maksym said, just use provided=true, or you can pass arguments to your @UiFactory method so it can adapt its behavior (but that means you have to put the argument on all listboxes in your ui.xml then).

Note however that setMultipleSelect has been de-deprecated in GWT 2.6, now that IE6 support is being sunset.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top