Question

I am new in gwt . I am devloping searching widget. i want when a user type in text box ,suggested string will be show to user and some character show in bold ,which are enter by the user like in google searching ..!enter image description here

please help me..

 TextBox fnametextbox=new TextBox();

    Label l = new Label("search");

    VerticalPanel p = new VerticalPanel();

    public void onModuleLoad() 
    {
            fnametextbox.addKeyPressHandler(new KeyPress());

            RootPanel.get().add(l);

            RootPanel.get().add(fnametextbox);
    }
    public class KeyPress implements KeyPressHandler
    {
        public void onKeyPress(KeyPressEvent event)      
        {
            String ab =fnametextbox.getText();

            if(ab.length()>1)
                {
                    greetingService.infouser(ab,new AsyncCallback<String>()
                            {
                                @Override
                                public void onFailure(Throwable caught) 
                                    {
                                        Window.alert("Invalid");
                                    }


                                @Override
                                public void onSuccess(String result) 
                                    { 
                                        System.out.println(result);

                                        if(result==gresult)
                                            { 
                                                System.out.println("result");

                                                p.clear();

                                                Label lii = new Label("not found");

                                                p.setVisible(true);

                                                p.setBorderWidth(1);

                                                p.add(lii);

                                                RootPanel.get().add(p);
                                            }
                                        else
                                            {
                                                p.clear();

                                                p.setVisible(false);

                                                p.setVisible(true);

                                                p.setBorderWidth(1);

                                                int l=0;

                                                for (String retval: result.split("/"))
                                                    {
                                                        if(l==0)
                                                            {
                                                                Anchor an = new Anchor(retval,
                                                                        false,
                                                                        "http://localhost:8080/w1/",
                                                                        "_blank");
                                                                        p.add(an);
                                                                        l++;
                                                            }
                                                        else 
                                                            {
                                                                Label an=new Label(retval);
                                                                p.add(an);
                                                                l--;
                                                            }

                                                        RootPanel.get().add(p);
                                                    }   
                                                }
Was it helpful?

Solution

Have you tried SuggestBox that provide this functionality in-build?

enter image description here


--EDIT--

use different constructor of Anchor that accepts SafeHtml.

  • Look at this constructor of Anchor

Sample code

String retval = "en<b>glish to hindi</b>";

Anchor anchor = new Anchor(retval, true, "http://localhost:8080/w1/", "_blank");

Sample code: (dynamically change the value in html)

String match = "en";
String result = "english to hindi/english speaking/english grammer/english songs";

for (String retval : result.split("/")) {
    String html = retval.replaceAll(match, "<b>" + match + "</b>");
    System.out.println(html);
    Anchor an = new Anchor(html, true, "http://localhost:8080/w1/", "_blank");
    ...
}

Note: Not a good design to fetch the result separated by slash. What about if slash is searched?

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