java 按钮:setEnabled(false) 不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12843613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Button: setEnabled(false) not working?
提问by brl8
I think I am just doing something stupid but I can't figure this one out.
我想我只是在做一些愚蠢的事情,但我无法弄清楚这一点。
I am using GWT, and I have a submit button where submit sends some information to a remote server via a REST API. The problem is, you can click submit multiple times while the operation is completing, and multiple posts are made.
我正在使用 GWT,我有一个提交按钮,提交按钮通过 REST API 向远程服务器发送一些信息。问题是,您可以在操作完成时多次单击提交,并且会发多个帖子。
I have tried adding
我试过添加
sendButton.setEnabled(false);
to the click handler, but it does not seem to be working. The button remains enabled and I can still click it as many times as I want resulting in multiple posts. Can someone see what I am doing wrong? Complete code below.
单击处理程序,但它似乎不起作用。该按钮保持启用状态,我仍然可以根据需要多次单击它,从而产生多个帖子。有人可以看到我做错了什么吗?完整代码如下。
public class HelpDeskTest implements EntryPoint {
private final HelpDeskTestServiceAsync helpDeskTest= GWT.create (HelpDeskTestService.class);
final Button sendButton = new Button("Submit");
final TextBox nameField = new TextBox();
final Label errorLabel = new Label();
final TextBox subjectField = new TextBox();
final TextArea descriptionField= new TextArea();
/**
* This is the entry point method.
*/
public void onModuleLoad() {
// We can add style names to widgets
//sendButton.addStyleName("sendButton");
// Add the nameField and sendButton to the RootPanel
// Use RootPanel.get() to get the entire body element
RootPanel.get("nameFieldContainer").add(nameField);
RootPanel.get("subjectFieldContainer").add(subjectField);
RootPanel.get("descriptionFieldContainer").add(descriptionField);
RootPanel.get("sendButtonContainer").add(sendButton);
RootPanel.get("errorLabelContainer").add(errorLabel);
//set name field text
nameField.setText("GWT User");
// Focus the cursor on the name field when the app loads
subjectField.setFocus(true);
subjectField.selectAll();
//set widths and heights
descriptionField.setWidth("100%");
descriptionField.setHeight("200px");
nameField.setWidth("100%");
subjectField.setWidth("100%");
//click handler
sendButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
sendButton.setEnabled(false);
String uName = nameField.getText();
String subject = subjectField.getText();
String desc = descriptionField.getText();
String newURLp1 = "http://xxx.xx.xx/sdpapi/request?" +
"OPERATION_NAME=ADD_REQUEST&TECHNICIAN_KEY=D4xxxxxxxB6" +
"&INPUT_DATA=<?xml version=";
String urlp2 = "%221.0%22";
String urlp3 = " encoding=";
String urlp4 = "%22utf-8%22";
String urlp5 = "?><Operation><Details><requester>" + uName + "</requester><subject>" + subject +
"</subject><description>" + desc + "</description></Details></Operation>";
String encUrl = URL.encode(newURLp1) + urlp2 + URL.encode(urlp3) + urlp4 + URL.encode(urlp5);
System.out.println(encUrl);
helpDeskTest.postToRemoteServer(encUrl,
new AsyncCallback<String>() {
@Override
public void onFailure(Throwable caught) {
Window.alert("Failure getting XML through proxy");
}
@Override
public void onSuccess(String result) {
processXML(result);
}
});
sendButton.setEnabled(true);
}
});
}
public void processXML(final String xml) {
try {
Document doc = XMLParser.parse(xml);
// get the status using Node's getNodeValue() function - this will determine success or failure.
String status = doc.getElementsByTagName("status").item(0).getFirstChild().getNodeValue();
//if success:
if (status.equals("Success")) {
String statCode = doc.getElementsByTagName("statuscode").item(0).getFirstChild().getNodeValue();
String msg = doc.getElementsByTagName("message").item(0).getFirstChild().getNodeValue();
String woid = doc.getElementsByTagName("workorderid").item(0).getFirstChild().getNodeValue();
System.out.println("Result from HelpDesk:");
System.out.println("Status Code: " + statCode);
System.out.println("Status: " + status);
System.out.println(msg);
System.out.println(msg + ". Ticket Number is: " + woid);
errorLabel.setText(msg + ". Ticket Number is: " + woid);
} else if (status.equals("Failed")){
//get message
String failmsg = doc.getElementsByTagName("message").item(0).getFirstChild().getNodeValue();
errorLabel.setText(failmsg);
}
}
catch ( Exception e ) {
e.printStackTrace();
}
}
}
}
采纳答案by Rohit Jain
sendButton.setEnabled(true);
You have enabled
your button again at the end of the onClick()
method.. See your click handler..
This might be the problem..
您enabled
在onClick()
方法的末尾再次拥有您的按钮.. 查看您的点击处理程序.. 这可能是问题所在..
Try moving this line inside onSuccess()
method: -
尝试在onSuccess()
方法中移动这条线: -
@Override
public void onSuccess(String result) {
processXML(result);
sendButton.setEnabled(true);
}