Hello,
I've got a JFrame which contains a handful of components. Is it
possible to have the JFrame respond to certain key-strokes.. ie.
ctrl-1, f1, etc, etc? Similar to how a menu item can have alt-f set to
bring up the file menu, regardless of what component has focus.
Thanks,
-Ken
|
|
0
|
|
|
|
Reply
|
khirmint
|
12/15/2004 5:21:46 PM |
|
What about adding the key listener to the JFrame?
|
|
0
|
|
|
|
Reply
|
klynn47
|
12/15/2004 10:53:30 PM
|
|
klynn47@comcast.net wrote:
> What about adding the key listener to the JFrame?
I tried that with a JFrame containing nothing but a JTextField. The
KeyListener on the JFrame didn't respond to any of the keystrokes. I
could add a KeyListener to all the objects within the JFrame, but that
seems a bit clunky?
Another example of what I'm after, is a dialog box that will close when
escape is pressed.
-Ken
|
|
0
|
|
|
|
Reply
|
Ken
|
12/16/2004 5:05:56 PM
|
|
/Ken/:
> klynn47@comcast.net wrote:
>
>> What about adding the key listener to the JFrame?
>
> I tried that with a JFrame containing nothing but a JTextField. The
> KeyListener on the JFrame didn't respond to any of the keystrokes. I
> could add a KeyListener to all the objects within the JFrame, but that
> seems a bit clunky?
>
> Another example of what I'm after, is a dialog box that will close when
> escape is pressed.
Does it make difference adding the key listener to the JRootPane of
the JFrame?
--
Stanimir
|
|
0
|
|
|
|
Reply
|
Stanimir
|
12/16/2004 9:58:50 PM
|
|
On Thu, 16 Dec 2004 11:05:56 -0600, Ken wrote
(in article <1103216756.197996.120160@f14g2000cwb.googlegroups.com>):
>
> klynn47@comcast.net wrote:
>> What about adding the key listener to the JFrame?
>
>
> I tried that with a JFrame containing nothing but a JTextField. The
> KeyListener on the JFrame didn't respond to any of the keystrokes. I
> could add a KeyListener to all the objects within the JFrame, but that
> seems a bit clunky?
>
> Another example of what I'm after, is a dialog box that will close when
> escape is pressed.
>
> -Ken
>
Here is how I do this.
AbstractAction act = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
myDialog.setVisible(false);
}
};
getRootPane().getActionMap().put("close", act);
int stdMask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
InputMap im =
getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "close");
--
Bill Tschumy
Otherwise -- Austin, TX
http://www.otherwise.com
|
|
0
|
|
|
|
Reply
|
Bill
|
12/16/2004 10:15:13 PM
|
|
Bill Tschumy wrote:
> On Thu, 16 Dec 2004 11:05:56 -0600, Ken wrote
> (in article <1103216756.197996.120160@f14g2000cwb.googlegroups.com>):
>
>
>>klynn47@comcast.net wrote:
>>
>>>What about adding the key listener to the JFrame?
>>
>>
>>I tried that with a JFrame containing nothing but a JTextField. The
>>KeyListener on the JFrame didn't respond to any of the keystrokes. I
>>could add a KeyListener to all the objects within the JFrame, but that
>>seems a bit clunky?
>>
>>Another example of what I'm after, is a dialog box that will close when
>>escape is pressed.
>>
>>-Ken
>>
>
>
> Here is how I do this.
>
> AbstractAction act = new AbstractAction()
> {
> public void actionPerformed(ActionEvent e)
> {
> myDialog.setVisible(false);
> }
> };
> getRootPane().getActionMap().put("close", act);
>
> int stdMask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
> InputMap im =
> getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
> im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "close");
>
>
Much better advice than the KeyListener suggestion, and the right way to
do it when you're using Swing components. I'd like to add one more point
to it though.
While this approach is correct when the action relates to the top level
component - i.e the window or the frame, it may be difficult or even
impossible to add the keybinding if the action relates to some component
that is sitting inside the window - especially when the component in
question is written as an independent class. This is because you'd
normally want to write the Action code in the class itself and specify
the keybinding in the constructor of the class, and tracing the
parentage back to the Frame/Window parent (in order to register it with
the RootPane) may be impossible at that point because the component may
not even have got added to the container hierarchy.
In such situations, it is best to add the KeyBinding to the
WHEN_IN_FOCUSED_WINDOW InputMap of the component. This way you don't
need to know anything about the top level Window at the time that you
apply the binding, but the binding will automatically start working once
the component has been added to the container hierarchy.
To the OP and the posters who suggested the KeyListener solution -
Please go through the Java Tutorial article on KeyBindings - it
describes InputMaps and ActionMaps in full detail.
BK
|
|
0
|
|
|
|
Reply
|
Babu
|
12/17/2004 6:13:37 AM
|
|
Bill Tschumy wrote:
> Here is how I do this.
>
> AbstractAction act = new AbstractAction()
> {
> public void actionPerformed(ActionEvent e)
> {
> myDialog.setVisible(false);
> }
> };
> getRootPane().getActionMap().put("close", act);
>
> int stdMask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
> InputMap im =
>
getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
> im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "close");
>
Thanks!
Thats exactly what I was looking for.
-Ken
|
|
0
|
|
|
|
Reply
|
Ken
|
12/20/2004 4:29:30 PM
|
|
|
6 Replies
259 Views
(page loaded in 0.088 seconds)
|