UiBinder with SuggestBox & MultiWordSuggestOracle

February 11th, 2010

I’ve been working on a somewhat large and complex GWT project using UiBinder over the past couple of weeks. I’ve built a number of widgets that use the SuggestBox and MultiWordSuggestOracle but I created a new UiBinder, populated the suggestions but they came up blank in the type-ahead. After about a half hour of scratching my head I looked back at some other code and figured it out. There are very few examples of the SuggestBox with UiBinder so I thought this might help someone out.

The UiBinder template (MyWidget.ui.xml) is fairly simple:

1
2
3
4
5
6
7
<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
    xmlns:g="urn:import:com.google.gwt.user.client.ui">
    <g:HTMLPanel>
        <g:SuggestBox ui:field="mySuggestBox"/>
    </g:HTMLPanel>
</ui:UiBinder>

The owner class (MyWidget.java) is where you need to make sure you have things correct. On line #20 you need to ensure you have (provided = true) or your suggestions will not show in the type-ahead. You also need to create your SuggestBox before you initialize the UiBinder;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.jeffdouglas;
 
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.Widget;
 
public class MyWidget extends Composite {
 
  private static MyWidgetUiBinder uiBinder = GWT.create(MyWidgetUiBinder.class);
 
  interface MyWidgetUiBinder extends UiBinder<widget, MyWidget> {
  }
 
  private final MultiWordSuggestOracle mySuggestions = new MultiWordSuggestOracle();
 
  @UiField(provided = true) // MAKE SURE YOU HAVE THIS LINE
  SuggestBox mySuggestBox;
 
  public MyWidget() {
    mySuggestBox = new SuggestBox(mySuggestions);
    initWidget(uiBinder.createAndBindUi(this));
    getSuggestions();
  }
 
  private void getSuggestions() {
    // call some service to load the suggestions
    mySuggestions.add("Suggestion #1");
    mySuggestions.add("Suggestion #2");
    mySuggestions.add("Suggestion #3");
  }
 
}


Categories: Code Sample, GWT

Leave a comment

Comments Feed13 Comments

  1. Jason

    Worked perfectly.. thanks for posting this – you saved me some frustration!

  2. Ale

    Thanks.

  3. Shawn

    I echo the above. Thanks for writing this.

  4. Dustin

    thanks for writing this. I’m surprised at how little Google has documented UiBinder. thanks a lot.

  5. Jeff Douglas

    I agree. Hopefully they will release more info after Google I/O.

  6. Vickie

    All of your tutorials have been very helpful, thanks!

  7. Draško

    I think the way how this should work is that:
    - no need to provide your SuggestBox
    - initilise the uiBinder
    - use getSuggestOracle method

    and than fill it up with your suggestions ;-)

    HTH

  8. berni

    Good example of flag provided
    thanks

  9. saurabh

    hi,
    i just copied this code. but in my project its not working .. can you tell me the coding that i need to write in the entry class.
    i m writing……
    public void onModuleLoad() {

    AutoSuggest autoSuggest = new AutoSuggest();
    Document.get().getBody().appendChild(autoSuggest.getElement());

    }

    where AutoSuggest is my uibinder class.

  10. slowpoison

    Thanks! You saved me quite a bit of trouble.

  11. Gizmo

    Thank you! Exactly what I needed :)
    I also learned what was this “provided = true” thing!

  12. sachin taware

    Hi this is good,can you post something which has aesthetic design with some links hooked to different pages populating data.I tried it using the Dockpanel but not successful as such in linking part.
    Thanks

  13. Dmitry Z.

    You saved my day!
    Thanks!

Leave a comment

Feed

http://blog.jeffdouglas.com / UiBinder with SuggestBox & MultiWordSuggestOracle

WordPress Appliance - Powered by TurnKey Linux