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
Developer
Posts: 27
Registered: ‎06-08-2009
My Device: Not Specified

Set default focus on a Field in screen

My requirement:

When a screen is displayed, a particular field in the screen should be given focus by default though there may be other "focusable" fields in the manager hierarchy above it.

 

Here is my current approach which doesn't seem to work:

 

public abstract class MyAbstractScreen extends MainScreen
{
    public abstract Field getDefaultFocusField();

    public void onDisplay()
    {
        Field field = getDefaultFocusField();
        if(field != null && field.isVisible())
        {
            field.setFocus();
        }
        super.onDisplay();
    }

}

public class MyScreen extends MyAbstractScreen
{
   EditField field1;
   EditField field2;
   EditField field3;

   public MyScreen()
   {
      add(field1);
      add(field2);
      add(field3);
   }

   public Field getDefaultFocusField()
   {
      return field3;
   }
}

 I would like field3 to have focus but it is always field1 that gets the focus when rendered.

 

Any help ?

 

Developer
Posts: 696
Registered: ‎12-02-2008
My Device: Not Specified

Re: Set default focus on a Field in screen

Hi,

 

I havnt tested this solution ,but i think this should solve the issue,

 

put statement super.onDisplay() as first statement and then write the code you have written in onDisplay().

Regards,
Rajat Gupta.
--------------------------------------------------------------------------------
If your problem was get solved then please mark the thread as "Accepted solution" and kudos - your wish
Developer
Posts: 27
Registered: ‎06-08-2009
My Device: Not Specified

Re: Set default focus on a Field in screen

Thanks. Tried but didnt work. One thing I missed mentioning is the paint method on MyAbstractScreen class. When I do a getLeadFieidWithFocus(), I get field1 and not field3..

Developer
Posts: 17,012
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Set default focus on a Field in screen

try to put the setFocus into an invokelater. it is possible that the framework sets the focus itself after you set it.

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter