How to ignore the focus of a cell in gridfieldmanager?

96 views Asked by At

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

1

There are 1 answers

3
Nate On BEST ANSWER

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.