Forums Register Login

how to give focus to a component ?

+Pie Number of slices to send: Send
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 ?
1
+Pie Number of slices to send: Send
KeyBindings
+Pie Number of slices to send: Send
 

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 ?
+Pie Number of slices to send: Send
KeyEventDispatcher may be what you're looking for.
+Pie Number of slices to send: Send


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.

+Pie Number of slices to send: Send
> 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.

 
1
+Pie Number of slices to send: Send
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.
My previous laptop never exploded like that. Read this tiny ad while I sweep up the shards.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2393 times.
Similar Threads
JTabbedPane Problem
How to change the tab order for jumping from one textfield to another
Focus Lost throws error
sdk1.4
Set colon mnemonic key for JLabel
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 01:52:08.