Search code examples
javazk

ZK - Check if an element has focus


Does ZK support checking if an element has focus?

HtmlBasedComponent has both focus() and setFocus(boolean) to set the focus.. but I don't see any getFocus().

Specifically, I am interested in checking the user's focus on an InputElement, a Bandbox; I want to know if the user is ready to type in the textbox part.


Solution

  • There is no method like isFocus() or getFocus(), but a workaround.
    InputElement has two events, onFocus and onBlur

    public class MyInputElem extends InputElement{
    
      private boolean focus = false;
    
      @Listen("onFocus")
      public void focus(){
        focus = true;
      }
    
      @Listen("onBlur")
      public void blur(){
        focus = flase;
      }
    
      public boolean isFocus(){
        return focus;
      }
    }
    

    So if you extend from a InputElement like this, it should keep
    the focus information up to date.