How to listen to the system clipboard changes?

A

Ahmed Moustafa

Hi All,

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?

Thanks in advance!

Ahmed
 
K

KC Wong

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?
 
A

Ahmed Moustafa

KC said:
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.
 
K

KC Wong

I searched Google with "Java Listen Clipboard" and found no results...
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.

What I said targets other applications... when your application has focus,
your application can handle all the events - including the PrintScr button.

So fire the clipboard checking when your application gains focus, or any
event that might affect the content of clipboard happened while your
application has focus.
 
Z

zoopy

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...istener(java.awt.datatransfer.FlavorListener)>

HTH,

Z.
 
S

Steve W. Jackson

zoopy said:
: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.html#
: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 =
 
A

Ahmed Moustafa

Steve said:
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
paste 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.
 
S

Steve W. Jackson

Ahmed Moustafa said:
: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
:paste 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.

That's true. But I never said otherwise, merely that you can get
automatic notification when other apps put data there when you'd
previously done so and thereby established ownership.

= Steve =
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,436
Messages
2,571,696
Members
48,796
Latest member
Greg L.
Top