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
Regular Contributor
heather_1
Posts: 83
Registered: ‎05-24-2011
My Device: Storm 9550
Accepted Solution

setFocus problems

Hi,

 

I am trying to set the focus on a button in a given screen. It seems like it should be as simple as using setFocus, but I am getting an IllegalStateException when setting the focus on my custom button by doing button.setFocus()

 

is there something I am missing?

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

Re: setFocus problems

As setFocus is updating the Ui, you have to do it holding the Event lock.  I guess you are updating this from a background Thread that does not have it.  The easiest thing to do is put your setFocus() in an Runnable, that will be executed on the Event Thread

 

UiApplication.getUiApplication()invokeLater(new Runnable() {

public void run() {

<field>.setFocus(0;

}

});

Regular Contributor
heather_1
Posts: 83
Registered: ‎05-24-2011
My Device: Storm 9550

Re: setFocus problems

I am trying to run that code, but I am getting a StackOverFlowError ? I'm new to BB and not sure what to do with that...

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

Re: setFocus problems

StackOverflowError will occur when you have some sort of recursive call t a method.  Running debug mode or finding the stack trace will tell you how this is happening. 

 

I'd say it was unlikely to be a result of this code.  I suspect instead that the error was always there and masked by the Exception you used to get. 

Regular Contributor
heather_1
Posts: 83
Registered: ‎05-24-2011
My Device: Storm 9550

Re: setFocus problems

Ok I got rid the stackoverflow error, but now the focus just isn't getting set? I am using the debugger and that line of code is being reached, but the focus isn't being set to the proper button. Since I am using custom buttons, am I supposed to override this method?

Regular Contributor
heather_1
Posts: 83
Registered: ‎05-24-2011
My Device: Storm 9550

Re: setFocus problems

I got it...thanks

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

Re: setFocus problems

[ Edited ]

If a setFocus is invoked, then that Field will get focus, assuming it is indeed focusable.  if the Field is not getting focus and it is focusable, then it means something else is taking the focus.

 

You need to determine which Field is getting focus and why.  We can't really help with this because the problem is in your code.

 

You might like to replicate the problem in a small section of code that contains only standard Fields and processing - if you do this, then we might be able to look at it. 

 

By default when pushed, the first focusable Field on a screen will get focus. 

 

Edit:

"Since I am using custom buttons"

I assume these are focusable?

Regular Contributor
heather_1
Posts: 83
Registered: ‎05-24-2011
My Device: Storm 9550

Re: setFocus problems

The custom buttons are focusable, so in the custom button class, I added the method

 

public void setFocus(){

super.setFocus();

}

 

and the code runs great.

 

Thanks for your help!