gpt4 book ai didi

java - GWT DialogBox 生成其自身的多个副本

转载 作者:行者123 更新时间:2023-12-01 14:58:24 26 4
gpt4 key购买 nike

下面是两个内部类,当用户单击相应的按钮时,它们会弹出一个简单的对话框。然后,他们更新填充界面上列表框的列表中的值。

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

    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();
}
}

最佳答案

您在类的 .onClick() 方法中创建一个对话框。请注意,您已将 Final 添加到对话框中,但这并不意味着新的 ClickHandler 不会多次添加到您的 saveButton 和 cancelButton 中。事实上,每次触发此 .onClick() 方法时,您都会构建此对话框。

更好的方法是这样的:

DialogBox editCustDialog;

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

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

关于java - GWT DialogBox 生成其自身的多个副本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14035427/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com