>Hi all,
>I have implemented the basic cut/copy/paste functionality in my Swing
[quoted text clipped - 12 lines]
>
>Thanks, Jonck
There doesn't seem to be anything like that, but you can check for
yourself at certain times. I assume you're listening for CaretEvents
to know when to enable or disable the cut and copy actions. You can
check the clipboard at the same time, and disable the paste action if
it doesn't contain data of the right flavor. It's not exactly
elegant, but it should work.
Alan Moore - 30 May 2005 11:33 GMT
>>Hi all,
>>I have implemented the basic cut/copy/paste functionality in my Swing
[quoted text clipped - 19 lines]
>it doesn't contain data of the right flavor. It's not exactly
>elegant, but it should work.
Correction: as of JDK 1.5, you can add a FlavorListener to the
Clipboard, and it will be notified whenever the set of DataFlavors
available from the clipboard changes. You still have to check for
yourself which flavors are available, but this would save you doing a
lot of pointless checks. If you're not running 1.5, of course, this
doesn't help you, but I tried the approach I suggested above, and it
seems to work just fine.
I have an editor that I wrote in Swing, in which I (like you) try to
disable actions when they aren't appropriate. So "cut" and "copy" are
disabled when there's no selection, but I had never bothered to
instrument the paste action that way. After posting the above I tried
it, and found it to be surprisingly easy. I was already updating the
UI whenever the caret moved or the app regained focus (among other
events), so all I had to do was add the following check to the paste
action:
clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor)
If I switch to Windows Explorer and copy a file to the clipboard, then
switch back to the editor, "paste" is disabled. Copy some text, and
it's enabled again.