2

i just made this example for the sake of my question.

public MyScreen()
{       
    setTitle("GridFieldManager");
    GridFieldManager grid2 = new GridFieldManager(1,2,GridFieldManager.FIXED_SIZE);

    add(grid2);

    String[] choice={"1","2","0"};
    ObjectChoiceField menu=new ObjectChoiceField(null,choice);

    //grid.add(lol);
    grid2.add(new LabelField("Productos_",NON_FOCUSABLE));
    grid2.add(menu);
}

For some reason i still can focus the labelfield even when i said it was non focusable (Field.NON_FOCUSABLE).

Note: Just to let you know, i'm using a label + objectchoicefield so i can have total control of the properties of that label, since i found troublesome changing the color and font from the label that objectchoicefield has.

In this picture you can see how the focus is on the LabelField. i just want to focus the objectchoicefield list.

enter image description here

Nate
  • 31,017
  • 13
  • 83
  • 207
AmirG
  • 615
  • 8
  • 18

1 Answers1

2

Honestly, to me, this seems like a bug. I don't think it should work this way. However, at this point, nothing in older BlackBerry Java devices is going to be fixed, so we just have to work around it.

The problem is that you have set your LabelField to be non-focusable, but the device is ignoring that. It is opening your screen with focus on the GridFieldManager, which just puts focus on its first field (the label). If the user moves focus onto the choice field, you won't be able to get focus back to the label field. But, I'm sure you don't want the screen opening with focus on something non-focusable.

You can either just set focus manually to the choice field:

  //grid.add(lol);
  grid2.add(new LabelField("Productos_", NON_FOCUSABLE));
  grid2.add(menu);  

  menu.setFocus();

or, if you want to handle this more generically, you can write your own method to initialize the focus to the first field that's really focusable:

public class LabelFocusScreen extends MainScreen {

   public LabelFocusScreen() {
      super(MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR);

      setTitle("GridFieldManager");
      GridFieldManager grid2 = new GridFieldManager(1,2,GridFieldManager.FIXED_SIZE);

      add(grid2);


      String[] choice={"1","2","0"};
      ObjectChoiceField menu=new ObjectChoiceField(null,choice);

      //grid.add(lol);
      grid2.add(new LabelField("Productos_", NON_FOCUSABLE));
      grid2.add(menu);  

      initializeFocus(this);
   }

   /** @return true if focus was set within the given field */
   private boolean initializeFocus(Field f) {
      boolean focusSet = false;
      if (f instanceof Manager) {
         Manager m = (Manager)f;
         // loop over all child fields, recursively
         for (int i = 0; i < m.getFieldCount(); i++) {
            if (initializeFocus(m.getField(i))) {
               focusSet = true;
               break;
            }
         }
      } else if (f.isFocusable()) {
         f.setFocus();
         focusSet = true;
      }
      return focusSet;
   }
}

Both solutions should work for you.

Note: by the way, you don't have to actually set the LabelField to be non-focusable. It will be non-focusable by default. It doesn't hurt. It's just not needed.


Update:

If you also want to be able to set focus back to grid2 later (after the screen first appears), you can also call my initializeFocus() method again, to prevent any more problems with the non-focusable label gaining focus.

For example, make your screen implement FocusChangeListener, and then in its constructor:

  grid2.add(new LabelField("Productos_", NON_FOCUSABLE));
  grid2.add(menu); 
  grid2.setFocusListener(this);

where the focus listener implementation includes this new method (in your screen class):

public void focusChanged(Field f, int event) {
   if (event == FocusChangeListener.FOCUS_GAINED) {
      initializeFocus(f);
   }
}

This will allow initializeFocus() to be called for the grid2 manager object, if focus is later set back to it. Calling initializeFocus(), passing the grid field manager as the argument, will correctly set focus to its first focusable field.

Community
  • 1
  • 1
Nate
  • 31,017
  • 13
  • 83
  • 207
  • hello sorry for taking so long to answer wasn't able to work. So what if i have 2 grids for example when i come back to the first grid the focus will be on the label field again. I think i need a focuslistener so i can set the focus to other field when this field has it or rewrite the "first focusable field" , is there a way to this ? – AmirG Jan 06 '14 at 12:48
  • @AmirG, I have updated my answer to handle this case. Basically, you do just add a focus change listener, and then have that listener call my `initializeFocus()` method for the grid field, to use the logic that searches for the first focusable field. I tested this, and it worked for me. – Nate Jan 08 '14 at 18:57
  • Thnx @Nate awesome as always – AmirG Jan 09 '14 at 00:46