Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to give focus to a component ?

 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
KeyBindings
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
KeyEventDispatcher may be what you're looking for.
 
Nitin Surana
Ranch Hand
Posts: 129
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 2542
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic