开发者

gwt add custom widget to panel

开发者 https://www.devze.com 2022-12-11 02:26 出处:网络
I have created some 开发者_开发问答classes in which it create a bunch of widgets (e.g. label, textbox). I want to create the widget on the fly and add it to a panel. How can I do that.Assuming you use

I have created some 开发者_开发问答classes in which it create a bunch of widgets (e.g. label, textbox). I want to create the widget on the fly and add it to a panel. How can I do that.


Assuming you use HorizontalPanel, VerticalPanel, FlowPanel or some other panel with an add(Wiget) method, you would simply invoke add(myWidget);

final VerticalPanel panel = new VerticalPanel();

final Button sendButton = new Button("Add widget");
panel.add(sendButton);
sendButton.addClickHandler(new ClickHandler() {

    @Override
    public void onClick(ClickEvent event) {
        panel.add(new Label(new Date().toString()));
    }
});

RootPanel.get().add(panel);

An alternative may be use the setVisible(boolean) to show and hide widgets instead of adding and removing them.

final VerticalPanel panel = new VerticalPanel();

final Button sendButton = new Button("Toggle visibility");
panel.add(sendButton);

final Label label = new Label(new Date().toString());
panel.add(label);

sendButton.addClickHandler(new ClickHandler() {

    @Override
    public void onClick(ClickEvent event) {
        label.setVisible(!label.isVisible());
    }
});

RootPanel.get().add(panel);
0

精彩评论

暂无评论...
验证码 换一张
取 消