in my application i get a component to focus , it could be a j开发者_如何学Cpanel , 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
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.
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
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.
精彩评论