How to make sure object is set before initWidget()

55 views
Skip to first unread message

Olar Andrei

unread,
Dec 12, 2016, 4:34:22 PM12/12/16
to GWT Users
Hello,

I'm creating a menu, and I basically need my UserInfo object already set (based on the username) before creating the menu and doing the initWidget().
Basically I have the username, and based on this, I query the DB and get everything else based on that username.
But I am using an RPC call for the backend part. How can I make sure that userInfo is set before proceeding to the createMenu() and initWidget() part ?

private static UserInfo userInfo;

public UserPanel() {
    container = new MaterialContainer();
  container.setFontSize("1em");

   setUserInfo("someUsername");

   Widget mainMenu = createMenu();
     initWidget(mainMenu);
}

public void setUserInfo(String username) {
    DBGetUserInfoAsync rpcService = (DBGetUserInfoAsync) GWT.create(DBGetUserInfo.class);
ServiceDefTarget target = (ServiceDefTarget) rpcService;
String moduleRelativeURL = GWT.getModuleBaseURL() + "DBGetUserInfoImpl";
target.setServiceEntryPoint(moduleRelativeURL);

rpcService.getUserInfo(username, new AsyncCallback<UserInfo>() {

        @Override
        public void onSuccess(UserInfo result) {
UserPanel.userInfo = result;
}

@Override
public void onFailure(Throwable caught) {
// Window.alert(caught.getMessage());
}
});
}


harshyadav

unread,
Dec 12, 2016, 5:00:23 PM12/12/16
to GWT Users
Call 
   Widget mainMenu = createMenu();
     initWidget
(mainMenu);
from the onSuccess() method of the RPC after you have set the userInfo object.


Something like:
rpcService.getUserInfo(username, new AsyncCallback<UserInfo>() {


         
@Override
         
public void onSuccess(UserInfo result) {
           
UserPanel.userInfo = result;

            Widget mainMenu = createMenu();
                          initWidget(mainMenu);
 
}



 
@Override

 
public void onFailure(Throwable caught) {
 
// Window.alert(caught.getMessage());
 
}
 
});


Remember, GWT RPC is asynchronous, so you have to always make sure you execute dependent code blocks in sequence.

Olar Andrei

unread,
Dec 12, 2016, 5:05:08 PM12/12/16
to GWT Users
Hello,

It does not work like this, I've already tried. It says "Error: java.lang.AssertionError: This UIObject's element is not set; you may be missing a call to either Composite.initWidget() or UIObject.setElement()" because my class extends Composite (I forgot to mention it). Like it is looking for the initWidget() method before executing the entire rpc call... 

harshyadav

unread,
Dec 12, 2016, 5:11:58 PM12/12/16
to GWT Users
Ok, if your class is extending a composite, then just make sure, the createMenu only initialize the ui elements.
After you have called the RPC, you then set the ui elements.

for e.g. 
private void setMenu(UserInfo userInfo) {
     label
.setName(userInfo.getName());
}

Olar Andrei

unread,
Dec 12, 2016, 5:17:05 PM12/12/16
to google-we...@googlegroups.com
I'm sorry. I don't get it here. The createMenu() should not return a Widget? Or?

--
You received this message because you are subscribed to a topic in the Google Groups "GWT Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-web-toolkit/28B788mIbEU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-web-toolkit+unsub...@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

harshyadav

unread,
Dec 12, 2016, 5:22:43 PM12/12/16
to GWT Users
Have the parts that create the widget and populate the widgets separate.

A better approach would be, as recommended by GWT to use MVP design pattern.

1) You create your view/composite/widget (View.java)
2) You call your RPC in a presenter (Presenter.java)
3) Your presenter than transfer the object to the view
4) The view just renders the object values

The documentation here below explains this in detail:

Olar Andrei

unread,
Dec 14, 2016, 2:57:51 PM12/14/16
to GWT Users
Hello,

I have done like you said it. But I have another problem now.

As you can see the setUsername() method sets the username, and by using a simple alert I've checked and it's correct.

BUT that method does not get called before the constructor, that means that initWidget() gets called first, and I'm using the username there, and it will be null since the setUsername method does not get called before initWidget().

What can I do here in order to have my username set in the initWidget() method.?

public class UserPanel extends Composite implements UserView {

private MaterialContainer container;

private Presenter presenter;

private String username;

        ...


        public UserPanel() {
container = new MaterialContainer();
container.setFontSize("1em");

Widget mainMenu = createMenu();
initWidget(mainMenu);
}

        ...
        
        @Override
public void setUsername(String username) {
this.username = username;
Window.alert(username);
}

@Override
public void setPresenter(Presenter presenter) {
this.presenter = presenter;
}



Reply all
Reply to author
Forward
0 new messages