Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / May 2005

Tip: Looking for answers? Try searching our database.

Clipboard listener?

Thread view: 
jonck@vanderkogel.net - 26 May 2005 21:40 GMT
Hi all,
I have implemented the basic cut/copy/paste functionality in my Swing
app, but I bumped into a problem that I have not been able to find an
answer to after extensive Googling and browsing the Swing API text.

The issue I'm having is how to know when the paste action should be
enabled and when it should not be. The cut and copy actions are easy,
since I can simply listen to whether a selection has been made or not.
But how do I know whether the paste action should be enabled? I was
expecting there to be something like a "ClipboardListener", but I was
unable to locate such a class or interface.

Could someone point me in the right direction how I should go about
this?

Thanks, Jonck
Alan Moore - 27 May 2005 04:42 GMT
>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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.