0

I am having problems while adding my widget into the RootPanel of another container class. I think that it may be related to the Asynchronous call that I make during the creation of the widget. I have a main class named ImageView which implements EntryPoint. In this class, I am creating an instance of my widget named NewWidget by clicking on a button. However, I cannot display it with the traditional methods:

Here is the EntryPoint class (ImageView):

import com.mycompany.project.client.widgets.NewWidget;
public class ImageViewer implements EntryPoint {

public void onModuleLoad() {

    Button b = new Button("Button");
    RootPanel.get().add(b);

    b.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
            NewWidget w = new NewWidget();
            RootPanel.get().add(w);
        }
    });
}

And here is my widget (NewWidget):

public class NewWidget extends Composite {

     final static DataServiceAsync service = (DataServiceAsync) GWT.create(DataService.class);

     final FlowPanel mainPanel = new FlowPanel();

     public NewWidget() {
         fillImagePath();
     }

     public void fillImagePath(){
     ServiceDefTarget endpoint = (ServiceDefTarget) service;
     endpoint.setServiceEntryPoint(GWT.getModuleBaseURL() + "data");
         service.getAllDocuments(new AsyncCallback<ArrayList<String>>() {
             @Override
             public void onFailure(Throwable e) {
                 Window.alert("Server call failed.");
             }
             @Override
             public void onSuccess(ArrayList<String> paths) {
                 process(paths);
             }
         });
     }

     public void process(ArrayList<String> paths){
         ArrayList<String> imagePath = paths;
         Image img = new Image(imagePath.get(4));
         mainPanel.add(img);
         initWidget(mainPanel);
     }
}

In this NewWidget, I am making an asynchronous call to my server in order to receive a String ArrayList, which contains 10 Strings that refer to the file path of 10 different images (i.e, "images/01.jpg", "images/02.jpg", and so on). I am pretty sure that I successfully and correctly receive these image paths. I have arbitrarily chosen index number 4 to display in my NewWidget.

The problem is that I cannot display this NewWidget in my ImageView main panel. I can easily display other widgets with this method. With many attempts, I have realized that I can display the image if I add the line RootPanel.get().add(mainPanel) at the end of NewWidget. However, I do not want to make a call that refers to the parent container (RootPanel in ImageView). Why can't I display this image with only instantiating it in my container panel, like I can display any other widget? I am pretty sure that it is related to the Asynchronous call not getting completed before I attempt to add the widget. But I don't know how to fix this.

I would be very glad if people would share their ideas. Thanks.

1 Answer 1

1

Move the initWidget(mainPanel) call to the first line the constructor.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.