Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: capturing the copy command



On Feb 27, 2004, at 12:16 PM, Erskine Williams wrote:

> Anyway, I'm trying to capture the command-C command in a custom JTable
> component. I've already a custom subclass of AbstractAction and
> configured with the following line:
>
> putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke('C',
> Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
>
> Then, I use this action to create a JMenu item by adding it to my edit
> menu. Pretty standard stuff.
>
> The problem is that when I hit "command-C" on my keyboard, my custom
> Action is not invoked. I can invoke the Action by clicking on the menu
> with the mouse, but invoking it with the configured KeyStroke
> accelerator doesn't work. If I change the accelerator char to
> something like 'L', then I can press "command-L" on my keyboard, and
> my Action will execute. Other, similarly-configured actions behave as
> expected - e.g. "command-N" executes a custom action to open a new
> window, "command-O" opens my custom file chooser dialog, etc. All of
> this is true of the Paste command as well, which I will also need to
> customize.
>
> So clearly, "command-C" (and "command-V") is already mapped to some
> component whose keymap is ahead of my component's keymap in whatever
> search order swing is using to find actions to perform (if I
> understand the mechanism correctly). So what do I need to do to hijack
> this behaviour?

Yes, cmd-C and cmd-V are handled by default as part of the Aqua L&F for
JTables. I believe what you want to do is call registerKeyboardAction
on your custom JTable instance:

table.registerKeyboardAction(myAction,
KeyStroke.getKeyStroke(KeyEvent.VK_C,
InputEvent.META_MASK),

JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

This will tell Swing to look in the component's action map first, and
it should find your action before the Aqua-default action.

We have similar code in the Aqua L&F classes, so while I haven't tried
this myself I'm pretty confident it will do what you want.

Hope that helps,
Scott


------------------
Scott Kovatch
Apple Computer
Java Runtime Classes
Cleveland Hts, OH
email@hidden

"Everybody needs money! That's why they call it money!"
_______________________________________________
java-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/java-dev
Do not post admin requests to the list. They will be ignored.


References: 
 >capturing the copy command (From: Erskine Williams <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.