Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
Posts: 17
Registered: ‎07-15-2011
My Device: 9300
My Carrier: Telcel
Accepted Solution

switch focus HorizontalFielManager

how it could change the focus of the fields manually, ie go to an area you choose, because when the HFM is the focus placed on the first but when you lose and return this pass to the last row

Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: switch focus HorizontalFielManager

Take a look at Field.setFocus() method. To detect where did you come from when your manager received focus, use Field.onFocus(int direction) 

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Contributor
Posts: 17
Registered: ‎07-15-2011
My Device: 9300
My Carrier: Telcel

Re: switch focus HorizontalFielManager

would be something like
hManager.getField (0). onFocus (1);??

Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: switch focus HorizontalFielManager

No, not at all! You should override onFocus to do additional things for you, not call it (it is called by the BB framework only).

Something like this:

protected void onFocus(int direction) {
  Runnable action = null;
  switch (direction) {
  case 1:
    action = new Runnable() {
      public void run() {
        getField(0).setFocus(); // focus the first field
      }
    };
    break;
  case -1:
    action = new Runnable() {
      public void run() {
        getField(getFieldCount() - 1).setFocus(); // focus the last
      }
    };
    break;
  case 0:
    break;
  default:
    break;
  }
  if (action != null) {
    UiApplication.getUiApplication().invokeLater(action);
  }
  super.onFocus(direction); // this one is questionable, remove if having problems
}

 

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Contributor
Posts: 17
Registered: ‎07-15-2011
My Device: 9300
My Carrier: Telcel

Re: switch focus HorizontalFielManager

I ran the code but still does not work because the super.onFocus (direction) replaces the focus of the Field that puts you, but if I remove it does not work

 

protected void onFocus(int direction) {
  Runnable action = null;
  switch (direction) {
  case 1:
    action = new Runnable() {
      public void run() {
        getField(10).setFocus(); // focus the first field
      }
    };
    break;
  case -1:
    action = new Runnable() {
      public void run() {
        getField(8).setFocus(); // focus the last
      }
    };
    break;
  case 0:
    break;
  default:
    break;
  }
  if (action != null) {
    UiApplication.getUiApplication().invokeLater(action);
  }
  super.onFocus(direction); // this one is questionable, remove if having problems
}

for example when you change to where a field is changed to 10 does not do it, but is positioned at the beginning

Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: switch focus HorizontalFielManager


qcfer35 wrote:

I ran the code but still does not work because the super.onFocus (direction) replaces the focus of the Field that puts you, but if I remove it does not work

... 

for example when you change to where a field is changed to 10 does not do it, but is positioned at the beginning


 I'm not sure I understand what you are saying here. Are you putting this code inside your HorizontalFieldManager?

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Contributor
Posts: 17
Registered: ‎07-15-2011
My Device: 9300
My Carrier: Telcel

Re: switch focus HorizontalFielManager

I do not think I explained well

 

I have my HorizontalFieldManager, which contains all the letters of the alphabet then that puts the focus on the HFM is the beginning, when the focus is lost by going to a component that is below it, put at the end of HFM then the I remember for example, is the focus of the letter where this before changing the focus

 

thanks for helping

Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: switch focus HorizontalFielManager

[ Edited ]

This can be done by making a custom manager extending HorizontalFieldManager like this:

public class HFMWithMemory extends HorizontalFieldManager implements FocusChangeListener {
  private Field _lastFocused;

  protected void onDisplay() {
    if (getFocusListener() == null) {
      setFocusListener(this); // could do this in constructor as well
    }
  }

  public void focusChanged(Field field, int eventType) {
    switch (eventType) {
    case FOCUS_CHANGED:
      UiApplication.getUiApplication().invokeLater(new Runnable() {
        public void run() {
          _lastFocused = getFieldWithFocus();
        }
      });
      break;
    case FOCUS_GAINED:
      if (_lastFocused != null) {
        UiApplication.getUiApplication().invokeLater(new Runnable() {
          public void run() {
            _lastFocused.setFocus();
          }
        });
      }
      break;
    case FOCUS_LOST:
      break;
    default:
      break;
    }
  }
}

I haven't tested the code, so there might be some typos, but overall it should work.

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Contributor
Posts: 17
Registered: ‎07-15-2011
My Device: 9300
My Carrier: Telcel

Re: switch focus HorizontalFielManager

the code works perfectly, only for them to serve, not implemented FieldChangeListener is implemented FocusChangeListener

and add the method onFocus (int direction);

 

adding this works perfectly

 

Thank you very much ARKADIZ

 

 

 protected void onFocus(int direction) {
        if(_lastFocused==null)
        {
            UiApplication.getUiApplication().invokeLater(new Runnable() {
                public void run() {
                  _lastFocused = getFieldWithFocus();
                }
              });
            super.onFocus(1);
        }
    }
Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: switch focus HorizontalFielManager

[ Edited ]

Actually, you don't need the separate onFocus override - do that in FOCUS_GAINED case. See that if (_lastFocused !=null)? Add an else clause to it and do what you do in your onFocus.

 

And yes, I made a typo (FieldChangeListener instead of FocusChangeListener) as I warned I might Smiley Happy.

 

[Edit]: I've edited the solution post to put implement FocusChangeListener there as it should have been...

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!