tags:

views:

71

answers:

3

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

A: 

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.

staticman
A: 

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

objects
+1  A: 

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.

ring bearer
so i get a jcompoenent and i need to deside what to call transferFocus orrequestFocusInWindowi know i will cal 'requestFocusInWindow' for a jbuttonand 'transferFocus' for a jpanel that holds buttons inside it by what i can decide what method to call
shay