public SimpleDialogSample() {
Button openButton = new Button("Open Dialog", new ClickHandler() {
public void onClick(ClickEvent event) {
final Dialog dialog = new Dialog(false, true);
dialog.setCaption("List");
VerticalPanel main = new VerticalPanel();
main.setWidth("100%");
dialog.setWidthPx(300);
dialog.setResizable(true);
Label label = new Label("Please select one of the items");
main.add(label);
main.setCellHeight(label, "25px");
final ListBox listBox = new ListBox(false);
for (String coreName : new String[] { "Item 1" }) {
listBox.addItem(coreName, coreName);
}
listBox.setVisibleItemCount(5);
listBox.setSelectedIndex(-1);
listBox.setWidth("90%");
listBox.setHeight("100%");
main.add(listBox);
main.setCellHorizontalAlignment(listBox, VerticalPanel.ALIGN_CENTER);
main.setCellHeight(listBox, "120px");
final SimpleButton okButton = new SimpleButton("OK", new ClickHandler() {
public void onClick(ClickEvent event) {
dialog.hide();
int index = listBox.getSelectedIndex();
String coreName = listBox.getValue(index);
}
});
okButton.setEnabled(false);
main.add(okButton);
main.setCellHorizontalAlignment(okButton, VerticalPanel.ALIGN_CENTER);
main.setCellHeight(okButton, "25px");
listBox.addChangeHandler(new ChangeHandler() {
public void onChange(ChangeEvent event) {
okButton.setEnabled(listBox.getSelectedIndex() >= 0);
}
});
dialog.setWidget(main);
dialog.setPopupPositionAndShow(new Popup.PositionCallback() {
public void setPosition(int offsetWidth, int offsetHeight) {
dialog.center();
}
});
final HandlerRegistration windowResizeHandlerRegistration = Window.addResizeHandler(new ResizeHandler() {
public void onResize(ResizeEvent event) {
dialog.center();
}
});
dialog.addCloseHandler(new CloseHandler<Popup>() {
public void onClose(CloseEvent<Popup> closeEvent) {
windowResizeHandlerRegistration.removeHandler();
}
});
}