in my application i get a component to focus , it could be a jpanel , and is could be a jbutton or a user custom made component
how can i know when to call transferFosus ,and when to call requestFocus
thanks you
in my application i get a component to focus , it could be a jpanel , and is could be a jbutton or a user custom made component
how can i know when to call transferFosus ,and when to call requestFocus
thanks you
It's rare that you would need to call either since its usually appropriate to let the user's keyboard/mouse actions determine focus. But transferFocus send focus away from your component and requestFocus brings focus to your component.
Use transferFocus() when you want to advance focus according to the focus order. requestFocus() is used to explicitly set the focus to a component.
Some background reading in Focus on Swing
transferFocus()
sends focus to the next component. Also note that
transferFocus()
will always transfer the focus in the forward direction.
requestFocus()
sends focus to calling component. However, there is no guarantee that this will be successful. Focus behavior is platform-dependent to certain extend.
The recommended mentod for gaining focus is to use requestFocusInWindow()
. Refer to this post - might come very handy in playing with focus.