GWT DialogBox会生成自己的多个副本

时间:2012-12-26 02:31:30

标签: java gwt dialog

以下是两个内部类,当用户单击相应按钮时会弹出一个简单的DialogBox。然后,他们更新填充接口上ListBox的List中的值。

我注意到,在大多数情况下,对话框似乎弹出了与ListBox的当前索引/选定值一样多的自身副本 - 但并非总是如此。有时只会弹出一个对话框。有任何想法吗?如果需要,我可以提供更多代码。

    public class EditCustomerHandler implements ClickHandler {
        public EditCustomerHandler() {}
        public void onClick(ClickEvent event) {

            final DialogBox editCustDialog = new DialogBox();
            editCustDialog.setHTML(customer.getName());

            FlexTable content = new FlexTable();
            FlexTable buttonPanel = new FlexTable();
            final TextBox customerNameTextBox = new TextBox();
            content.setText(0, 0, "Name: ");
            content.setWidget(0, 1, customerNameTextBox);

            Button saveButton = new Button("Save");
            saveButton.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                    if (!customer.getName().equals(customerNameTextBox.getText())) {
                        customer.setName(customerNameTextBox.getText());
                        editCustDialog.hide();
                        stationService.saveCustomer(customer, new DefaultAsyncCallback<String>() {
                            public void onSuccess(String errorCode) {
                                if (errorCode != null) {
                                    MessageBox.showMessage("Error", errorCode);
                                    return;
                                }
                                refreshCusts();

                            }
                        });
                    }
                    }               
            });
            Button cancelButton = new Button("Cancel");
            cancelButton.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                    editCustDialog.hide();                  
                }               
            });

            buttonPanel.setWidget(0, 0, saveButton);
            buttonPanel.setWidget(0,1, cancelButton);
            content.getFlexCellFormatter().setColSpan(1, 0, 2);
            content.getCellFormatter().setHorizontalAlignment(1,0, HasHorizontalAlignment.ALIGN_CENTER);
            content.setWidget(1,0, buttonPanel);
            editCustDialog.add(content);
            editCustDialog.center();
            editCustDialog.setGlassEnabled(true);
            editCustDialog.setModal(false);
            editCustDialog.show();

        }
    }

    public class PlusCustomerHandler implements ClickHandler {
        public PlusCustomerHandler() {}
        public void onClick(ClickEvent event) {
            final DialogBox plusCustDialog = new DialogBox();
            plusCustDialog.center();
            plusCustDialog.setModal(false);
            plusCustDialog.setGlassEnabled(true);
            plusCustDialog.setHTML("New Customer");
            final TextBox customerNameTextBox = new TextBox();
            final FlexTable content = new FlexTable();
            final FlexTable buttonPanel = new FlexTable();
            final Button saveButton = new Button("Save");
            saveButton.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                    if (!customerNameTextBox.getText().equals("")) {
                    SMSCustomer newCustomer = new SMSCustomer();
                    newCustomer.setName(customerNameTextBox.getText());
                    stationService.saveCustomer(newCustomer, new DefaultAsyncCallback<String>() {
                        public void onSuccess(String errorMsg) {
                            if (errorMsg == null) {
                                refreshCusts();
                                plusCustDialog.hide();
                            }
                        }

                    });
}
                    else {
                        MessageBox mb = new MessageBox();
                        mb.showMessage("Yo dawg", "Customers must have names!");

                    }
                    }});

            Button cancelButton = new Button("Cancel");
            cancelButton.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                    plusCustDialog.hide();                  
                }               
            });

            content.setText(0, 0, "Name: ");
            content.setWidget(0, 1, customerNameTextBox);
            buttonPanel.setWidget(0, 0, saveButton);
            buttonPanel.setWidget(0,1, cancelButton);
            content.getFlexCellFormatter().setColSpan(1, 0, 2);
            content.getCellFormatter().setHorizontalAlignment(1,0, HasHorizontalAlignment.ALIGN_CENTER);
            content.setWidget(1,0, buttonPanel);
            plusCustDialog.add(content);


            plusCustDialog.show();
            }
        }

1 个答案:

答案 0 :(得分:1)

您可以在班级的.onClick()方法中创建一个对话框。请注意,您在对话框中添加了final,但这并不意味着不会多次向saveButton和cancelButton添加新的ClickHandler。实际上,每次触发.onClick()方法时都会构建此DialogBox。

更好的方法是这样的:

DialogBox editCustDialog;

public void onClick(ClickEvent event) {
    if (editCustDialog == null) {
        buildEditCustDialog();
    }
    editCustDialog.show(); // or, editCustDialog.center();
}

private void buildEditCustDialog() {
    editCustDialog = new DialogBox();
    // etc.
}