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: 275
Registered: ‎03-10-2009
My Device: Not Specified

how do i tell if ListField has focus?

[ Edited ]

my MainScreen's content changes over time. it starts with a form, and then possibly more than one ListField. I wanted to do a certain action if a user clicks the trackball. I tried doing the following:

 

1. on my list, i highlighted one row

2. i clicked the trackball, and i have this code:

 

 

public boolean navigationClick(int status, int time) {
if (myList.isFocus()) {
System.out.println("yay!");
return true;

}

return false;
}

 

 

but this does not work. isFocus() is always returning false. 

 

what is the correct way of handling this? in the future I may have more than one ListFields. how do i know which one the user has clicked on?

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: how do i tell if ListField has focus?

To do this I have always extended the ListField, then used an isFocused() method as follows:

 

class MyListField extends ListField {
    boolean _isFocused = false;

    //Invoked when this field receives the focus.
    public void onFocus(int direction) 
    {
        _isFocused = true;
        super.onFocus(direction);
    }

    //Invoked when a field loses the focus.
    public void onUnfocus() 
    {
        _isFocused = false;
        super.onUnfocus();
    }

    public boolean isFocused() {
        return _isFocused;
    }
}

 

Developer
Posts: 275
Registered: ‎03-10-2009
My Device: Not Specified

Re: how do i tell if ListField has focus?

thank you Peter. I will give it a shot and report back!

Developer
Posts: 70
Registered: ‎05-04-2009
My Device: Blackberry 9800/Blackberry 9530/Blackberry 9700/Blackberry 8530
My Carrier: T-Mobile/Wifi

Re: how do i tell if ListField has focus?

Thank you very much this was very helpful. I spent about an hour trying to debug why isFocus() was always returning false.

If you find this post useful please give kudo's
Highlighted
Developer
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: how do i tell if ListField has focus?

I wonder if

if (myList == getLeafFieldWithFocus()) {

  ....

}

would work in place of Peter's trick.




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.