2012-10-11 2 views
4

나는 바보 같은 짓을하고 있다고 생각하지만이 것을 알아낼 수는 없다.버튼 : setEnabled (false)가 작동하지 않습니까?

저는 GWT를 사용하고 있으며 제출 버튼을 사용하여 제출 정보가 REST API를 통해 원격 서버에 전송됩니다. 문제는 작업이 완료되고 여러 게시물이 작성되는 동안 제출을 여러 번 클릭 할 수 있다는 것입니다.

나는 클릭 처리기에

sendButton.setEnabled(false); 

를 추가하려했지만, 제대로 동작하지 않습니다. 버튼은 계속 활성화되어 있으며 원하는만큼 여러 번 클릭하여 여러 게시물을 볼 수 있습니다. 누군가 내가 잘못하고있는 것을 볼 수 있습니까? 아래 코드를 완료하십시오.

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


    } 

}

+0

당신은 ENABL 작업이 끝난 후에'sendButton'을 다시 호출하십시오. 실행 중에 버튼이 비활성화되지 않았습니까? – Baz

답변

3
sendButton.setEnabled(true); 

당신은 onClick() 방법의 끝에 enabled 당신의 버튼을 다시해야 ..

옮겨보십시오 ..이 문제가 될 수 있습니다 .. 당신의 클릭 핸들러를 참조하십시오 onSuccess() 방법 안쪽이 라인 : -

@Override 
public void onSuccess(String result) { 
    processXML(result); 
    sendButton.setEnabled(true); 
} 
+1

왜 "."대신 ".."을 항상 사용합니까? – Baz

+0

@Baz. Habbit :) 내 손이 내게 잔인 해. 또는 오히려 그들은 통제 불능입니다. : P –

+0

그랬어. 고맙습니다! – brl8

관련 문제