Copy n Paste in swing

Um, how do you do this …using Actions?

It’s one of those many evil parts of AWT/swing design where it’s like learning French - the most frequently-used stuff to seems to be the most deviating from all the rules :(.

What I do know is this:

  • Actions which register themselves as using CTRL + C or V as their ACCELERATOR_KEY get silently ignored. But if you use any other CTRL combination, it works as a global shortcut.
  • I can’t see anything in the Action docs as to why this happens (or even any indication that it happens). In fact, the AbstractAction docs even use “Cut” as an example (haven’t checked, but I’m guessing Ctrl-X does nothing too?)
  • …but I also can’t see what the difference is between ACCELERATOR_KEY and ACTION_COMMAND_KEY.
  • …except that if you attempt to put a value of Ctrl-C for ACTION_COMMAND_KEY, you crash swing’s AbstractAction class. It dies with an internal ClassCastException. Great!

There’s also TextAction:
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/text/TextAction.html

Oh and there is also a little demo with copy/past/cut
/demo/jfc/Notepad

Sorry, I should have been clearer…

I want to do copy/paste that involves no text whatsover at all.

:).

e.g. copy/pasting objects and things. So, no text components are involved, and in fact all the components are custom subclasses of JPanel (or similar).

Which makes it all the more annoying that CTRL-C simply doesn’t work - assuming this is a design choice and not a bug (despite the lack of docs) - that it seems to be in order to support some text handling that is totally irrelevant! :frowning:

Hmhmhmhmhm…

Maybe KeyMap helps then?

Keymap
Defines the Actions performed when certain keys are pressed. For example, pressing CTRL-C may copy some text and CTRL-V may paste the cut or copied text and the current cursor location. This is considered a look-and-feel feature because different native look-and-feels have different default keymaps.”

Hmyea… default keymaps are to blame, I guess :slight_smile:

[quote]Hmhmhmhmhm…

Maybe KeyMap helps then?
[/quote]
Maybe. I’ve tried manually altering the KeyMap’s before for stuff like this, but it always seems to break Swing.

In particular, I tend to have a heck of a hard time trying to remove the seemingly random places and situations where Sun’s Swing components insert their own bindings - and I’ve not managed to find a clear simple list of what’s doing what where :(.

…is this stuff perhaps stored authoritatively in e.g. the Look-n-feel descriptors? But then I run into the problem that Sun still hasn’t documented LaF API’s etc, so I don’t know what I’m looking at or where to look for it (the “where” being easier to workaround - I’ve found LaF data before, but never found any docs).

Any advice on altering keymaps?

NB: just inserting a new default keymap at the root of the keymap chain is something I found relatively easy before.

HOWEVER IIRC even this breaks Swing! There seem to be swing components that rely upon the fact that some things are not in the KeyMap and then handle them specially when they aren’t found. Or something like that. I posted in Newbies because I’ve never quite got my head around this stuff (partly because it keeps changing from release to release :stuck_out_tongue: ?) and am probably misunderstanding a lot of it :(.