Always focus on Jframe

Asked

Viewed 933 times

1

I need that when I click on some button of my application the focus returns to the JFrame. Is there any method for this?

I could call the method MeuFrame.requestFocus() in each ActionPerformed, but that would take too long.

1 answer

1

There is yes, try:

You tell your jButton transfer focus and ask for your jFrame solicit this focus.

 jButton.transferFocus();
 jFrame.requestFocus();

You can still get focus by forcing through the:

jFrame.grabFocus();

This method gets the focus regardless of who has it (forces the transfer of focus to you), including brings the focus to the window if it is not active.

I would recommend you use the first way, because the effect of the second may be unwanted to the user.

  • there are many buttons on my app... it would take too long...

  • there’s no other way?

  • each button of yours must say that it must transfer

  • @Dongabi your buttons don’t have an action? Just put at the end of the action.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.