5

Hi is there any way to create something similar to Window.alert() in GWT? Basically I wanted to customize the Window.alert()'s "Ok" button to say something else but as I researched there is no way to customize the alert boxes.

Thanks.

Alberto Zaccagni
  • 30,779
  • 11
  • 72
  • 106
user_1357
  • 7,766
  • 13
  • 63
  • 106

3 Answers3

14

Window.alert() is already available in GWT. It opens a native dialog box which contais OK button localized by the browser's locale. This alert box can not be changed.

Use PopupPanel or DecoratedPopupPanel.

Peter Knego
  • 79,991
  • 11
  • 123
  • 154
0

You could use the PopupPanel.

Alberto Zaccagni
  • 30,779
  • 11
  • 72
  • 106
0

I usually code a generic dialog box, that is created once and when I need it again the html content and title is replaced. You can also add a OK/Cancel button combination, this all is rather straightforward.

private DialogBox   dialog     = null;
private HTML        dialogHtml = new HTML();
public void onDialog(final String title, final String html) {
  if (dialog == null) {
    dialog = new DialogBox();
    dialog.getElement().getStyle().setZIndex(99);
    dialog.setWidth("500px");
    dialog.setGlassEnabled(true);
    dialog.setAnimationEnabled(true);
    dialog.setModal(true);
    VerticalPanel vp = new VerticalPanel();
    vp.add(dialogHtml);
    HorizontalPanel hp = new HorizontalPanel();
    hp.setWidth("100%");
    Button close = new Button("close");
    close.setWidth("200px");
    close.addClickHandler(new ClickHandler() {
      @Override
      public void onClick(ClickEvent event) {
        dialog.hide();
      }
    });
    hp.add(close);
    hp.setCellHorizontalAlignment(close, HasHorizontalAlignment.ALIGN_CENTER);
    hp.getElement().getStyle().setMarginTop(40, Unit.PX);
    vp.add(hp);
    vp.setSpacing(10);
    dialog.add(vp);
  }
  dialogHtml.setHTML(html);
  dialog.setHTML(title); // the actual title
  dialog.show();
  dialog.center();
}

The HTML content is something very simple, i.e.

<div style="width: 500px; overflow: auto;">...</div>
Harald Schilly
  • 1,098
  • 1
  • 14
  • 15