GWT IFrame DialogBox example

In GWT, being able to load external pages comes in extremely handy when you want to pass on a lot of the design work to another team. This can be achieved by loading an external page elsewhere on the web server into an IFrame and wrapping the existing widgets.

Most GWT widgets can be wrapped, buttons, DIV’s, check boxes, etc.

Unfortunately I’ve not found a way of wrapping a HTML table. The only way around that is to have the table on the existing HTML page in a DIV, clear the DIV and programmatically add the new table which can be styled using the pages CSS. However, another problem arises with CSS which is you cannot use table header and footers CSS.

Anyway, below is a quick and simple way of loading an external page into a DialogBox using an IFrame and wrapping the buttons. You might notice that I’ve cleared the DialogBox CSS style.

    private void go_reset_all_accounts() {
		final String id_cancel = "cancel";
		final String id_okay = "okay";
		final String user = Cookies.getCookie(cookie_username);
		
		final DialogBox dia = new DialogBox();
		final Frame frame = new Frame(url_base + url_ext_resetaccounts);
		frame.addLoadHandler(new LoadHandler() {
			
			@Override
			public void onLoad(LoadEvent event) {
				Document doc = IFrameElement.as(frame.getElement()).getContentDocument();
				
				Button cancel = Button.wrap(doc.getElementById(id_cancel));
				Button okay = Button.wrap(doc.getElementById(id_okay));
				
				cancel.addClickHandler(new ClickHandler() {
					
					@Override
					public void onClick(ClickEvent event) {
						dia.hide();
					}
				});
				
				okay.addClickHandler(new ClickHandler() {
					
					@Override
					public void onClick(ClickEvent event) {
						server.Reset_Accounts(user, new AsyncCallback<Boolean>() {

							@Override
							public void onFailure(Throwable caught) {
								dia.hide();
								log.info("Server communication error");
							}

							@Override
							public void onSuccess(Boolean result) {
								dia.hide();
								if (result) {
									log.info("Accounts have been wiped clean");
									Window.Location.assign(url_base + url_login);
								} else {
									log.info("Server refused to wipe accounts. Permission denied.");
								}
							}
						});
					}
				});
			}
		});
		
		frame.setSize("500px", "200px");
		dia.add(frame);
		dia.setGlassEnabled(true);
		dia.setStyleName("");
		dia.center();
		dia.show();
	}

Leave a Reply

Your email address will not be published.