:Steve W. Jackson wrote:
:> In article <
[email protected]>,
:>
:>
:>>:On 9-7-2004 8:15, Ahmed Moustafa wrote:
:>>:> KC Wong wrote:
:>>:>
:>>:>>> I have a paste button in a toolbar, I need to enable and disable the
:>>:>>> button based on the contents of the clipboard.
:>>:>>>
:>>:>>> I implemented a thread which polls the clipboard every 1 second and
:>>:>>> updates the status of the paste button. Everything worked fine on my
:>>:>>> Windows XP machine but the my Java application killed my Linux
:>>:>>> machine!
:>>:>>>
:>>:>>> Is there an alternative mechanism to handle the toolbar paste button?
:>>:>>
:>>:>>
:>>:>>
:>>:>> I searched Google with "Java Listen Clipboard" and found no results...
:>>:>>
:>>:>> But since your user must have focused to another application to put
:>>:>> things
:>>:>> into clipboard, how about checking only when your application gains
:>>:>> focus?
:>>:>
:>>:>
:>>:> The button should be enabled only if the contents of the clipboard is of
:>>:> string flavor.
:>>:>
:>>:> So, this approach will not work with the following scenario: the paste
:>>:> button is enabled and the application has the focus then the user hits
:>>:> "Print Screen", that will put an image in the clipboard and the button
:>>:> should have been disabled.
:>>:>
:>>:You can add a FlavorListener to the Clipboard, but unfortunately this is
:>>:not
:>>:available prior to J2SE
:>>:1.5 (or should I say J2SE 5.0 ;-)
:>>:
:>>:<
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/datatransfer/Clipboard.ht
:>>:ml#
:>>:addFlavorListener(java.awt.datatransfer.FlavorListener)>
:>>:
:>>:HTH,
:>>:
:>>:Z.
:>
:>
:> Don't forget that the ClipboardOwner interface includes a lostOwnership
:> method which will automatically tell you (after you put something onto
:> the clipboard) when another application copies to it. In this way, you
:> can at least omit any form of polling after your own application puts
:> data there until you receive this notification.
:>
:> = Steve =
:
:If my application lost the clipboard ownership to another application
:which put an image in the clipboard and I got notified and disabled the

aste button, then the same other application put text in the clipboard,
:I would not be notified and I would not be able to enable the button again.