Search code examples
componentszk

Disable a button before calling a method


I want to disable a button before calling a method.
In other words, I want to execute some codes as linear.

Here is the code

@Listen("onClick = #kodIsteButtonId")
public void kodIste() {
   k = kd.findKisiByKullaniciadi(kullaniciadiId.getValue());
   if (k == null) {
         Messagebox.show("Böyle bir kullanıcı bulunamadı!", "Hata Mesajı",
            0, Messagebox.ERROR);
    } else {
         kodIsteButtonId.setDisabled(true);
         countDown(10, 6, k);
    }
}

private void countDown(int fromToZero, int sleepTime, Kisiler k) {
  tempPassword = rasgeleYeniSifre();
  sendEposta(k.getEposta(), tempPassword);
  alert("Güvenlik kodu e-postanıza gönderildi.");
  while (fromToZero > 0) {
    try {
        Thread.sleep(sleepTime * 1000);
    } catch (Exception e) {
        e.printStackTrace();
    }
    fromToZero--;
}
   sifreAlButtonId.setDisabled(false);
   clearButtonId.setDisabled(false);
   kodId.setDisabled(false);
}

When I click the button I want to see it disabled.
But after running the countDown() method, it is still enabled.

What could be the reason?


Solution

  • The reason is that the button disable the moment you get an answer back from the server.
    For your case this is the moment that public void kodIste() is finished.

    The most easiest solution is to use the autodisable :

    <button autodisable="self" />
    

    This disable and enable the button automaticly.
    If you want to control when the button has to enable itself again you can do :

    <button autodisable="+self" />
    

    Documentation can be found here.