0

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

我注意到,在大多数情况下,对话框似乎会弹出与 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();
            }
        }
4

1 回答 1

1

您在类的方法内创建一个对话框.onClick()。请注意,您在对话框中添加了 final,但这并不意味着新的 ClickHandler 不会多次添加到您的 saveButton 和 cancelButton。实际上,每次.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.
}
于 2012-12-26T03:40:40.753 回答