This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Swing / AWT / SWT and the fly likes how to give focus to a component ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "how to give focus to a component ?" Watch "how to give focus to a component ?" New topic
Author

how to give focus to a component ?

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

what i want is
when my textfield is not in focus , by pressing alt+I my textField should get focus

one long way to do this is
i need to register focus listener on all my components , ie menu items , tool bars, etc so that when any of them is in focus and user press this alt+I textField should receive focus
do any one has any other way to do the same ?


The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
KeyBindings
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

Michael Dunn wrote:KeyBindings


straight from the specs
The JComponent class supports key bindings as a way of responding to individual keys typed by a user. Here are some examples of when key bindings are appropriate:
You're creating a custom component and want to support keyboard access to it.
For example, you might want the component to react when it has the "focus" and the user presses the Space key.


but my textfield doesn't have focus so what then ?
marwen Bakkar
Ranch Hand

Joined: Jan 28, 2010
Posts: 84
KeyEventDispatcher may be what you're looking for.
Nitin Surana
Ranch Hand

Joined: Jan 21, 2011
Posts: 129



It sets the key accelerator that will cause the receiving text component to get the focus. The accelerator will be the key combination of the alt key and the character given (converted to upper case). By default, there is no focus accelerator key. Any previous key accelerator setting will be superseded. A '\0' key setting will be registered, and has the effect of turning off the focus accelerator. When the new key is set, a PropertyChange event (FOCUS_ACCELERATOR_KEY) will be fired.

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> but my textfield doesn't have focus so what then ?

you needed to read further down the page


JComponent.WHEN_IN_FOCUSED_WINDOW
The component's window either has the focus or contains the component that has the focus. This input map is commonly used for mnemonics or accelerators, which need to be active regardless of where focus is in the window.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2177
    
    7
I like to use label / text field pairs for this. Something like:



Behind the scens the Key Bindings will be created automatically for you and you have a self documenting interface where Alt-F can be used to set focus on the text field.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to give focus to a component ?