GWT composite widget

守給你的承諾、 提交于 2019-12-13 04:59:35

问题


I try to build own GWT widget. I select the composite way of build it. But I have problem, the widget didn't see on screen. Here is the composite widget code:

public class LoginPanel extends Composite {

private static final String DEFAULT_STYLENAME = "LoginPanel";

private TextBox nameField;
private PasswordTextBox passField;
private Button loginButton;
private Label errorLbl;
private DecoratedPopupPanel decoratedPopupPanel;

public LoginPanel() {
    nameField = new TextBox();
    passField = new PasswordTextBox();
    loginButton = new Button("Login");
    loginButton.getElement().setId("loginButton");
    errorLbl = new Label();

    Grid grid = new Grid(4, 2);
    grid.setWidget(0,0, errorLbl);
    grid.setWidget(1,0, new Label("Name"));
    grid.setWidget(1,1, nameField);
    grid.setWidget(2,0, new Label("Password"));
    grid.setWidget(2,1, passField);
    grid.setWidget(3,1, loginButton);

    decoratedPopupPanel = new DecoratedPopupPanel();
    initWidget(decoratedPopupPanel);
    //decoratedPopupPanel.setAnimationEnabled(true);
    decoratedPopupPanel.setWidget(grid);
    //decoratedPopupPanel.setAnimationEnabled(true);
    decoratedPopupPanel.setPopupPositionAndShow(new PopupPanel.PositionCallback() {
        public void setPosition(int i, int i1) {
            decoratedPopupPanel.setPopupPosition(Window.getClientWidth()/2, Window.getClientHeight()/2);
        }
    });
}

}

In entry class function onModuleLoad() I have this peace of code:

LoginPanel lp = new LoginPanel();
RootPanel.get().add(lp);

When I delete in LoginPanel class rows behind initWidget() function, then all is right, but widget is not centered. Can anyone help me?


回答1:


The problem is a PopupPanel can't be used as a 'normal' widget, because it won't be attached to a parent but it attaches itself to the dom to make guarantee it's on top. So using initWidget on a popup doesn't make sense.

To make this work you should put the LoginWidget in the PopupPanel instead of the other way around. You could make the PopupPanel a member field and add a method show to your LoginPanel that calls the show on the PopupPanel field.

If you don't want the login widget to float independent of a parent or context you're using the Login widget in, you shouldn't use a PopupPanel.




回答2:


I believe you have to call initWidget as the last thing. Otherwise all the processing of 'children' items doesn't have anything to work against.

Try that, and if that doesn't work, I'll try and give a better answer.



来源:https://stackoverflow.com/questions/5168917/gwt-composite-widget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!