Support Notifications of Clipboard Contents Changes
request for enhancement
Most Windows applications, and probably those on other platforms,
typically enable "paste" functionality only when there is valid data
on the clipboard ("valid" meaning of an appropriate flavor).
Implementing this behavior requires a mechanism through which changes
to the clipboard can be detected. That is when the option should be
enabled or disabled -- when the clipboard contents change. The Java
Clipboard API does not currently support this type of notification,
but it could be added trivially.
I suggest that java.awt.Clipboard be extended to support a set of
listeners (PropertyChangeListener would be adequate) who are notified
when the clipboard contents change. This would be entirely backward
compatible with the existing API and would provide very useful
functionality that is not currently available.
(Review ID: 93418)
I am not aware of any workaround. I tried a hack using
ClipboardOwnership. The idea was that if I am always the owner
of the clipboard, I will receive a lostOwnership whenever the
contents change. However, it is not generally possible (or good)
to steal ownership when another application is using the
Will consider along with other Clipboard RFEs for merlin.
We didn't have time to get this in for merlin. Will have to wait for tiger.
Notifications on clipboard contents changes can be
implemented for a local clipboard and the Windows system
clipboard, but not for selections on Solaris/Linux, since
Xlib does not provide any means for doing that.
To determine whether notifications on contents changes are
supported for the clipboard, the method
Clipboard.areContentsChangeNotificationsSupported() has been
In order to be notified on a clipboard contents changes the
ContentsListener should be added to the clipboard by means
of Clipboard.addContentsListener() method. Also public two
methods for managing listeners were added:
I should mention that first I have tried to implement
notifications on clipboard contents changes using
PropertyChangeListeners. In that case old and new contents
of a clipboard must be passed to listeners.
It is trivial to implement it for a local clipboard.
As for Windows system clipboard, it could be implemented,
but a great care should be taken to synchronize contents
updates (this is not that problematically), also new
clipboard contents can not be reliably retrieved. If some
application opened the system clipboard and did not close
it, an attempt to fetch clipboard contents would fail, and
the failure would manifest in our code, where it could not
be handled properly.
So the decision not to provide clipboard contents directly
to the listeners and to use a new interface for listeners
(java.awt.datatransfer.ContentsListener) and a new class for
events (java.awt.datatransfer.ContentsEvent) was taken.
The described above implementation of the feature isn't
really cross-platform: the feature wasn't implemented for
the system clipboard and system selection on X11 platforms.
All that we could do on X11 platforms was polling a
selection, retrieving its contents and comparing that
contents with the previous one. It isn't acceptable since it
is very expensive.
So in order to properly implement this feature on all
supported platforms we decided to redesign the current
implementation. To implement the functionality defined in
the description (enabling/disabling paste option in a Java
application depending on what flavors clipboard data are
available) it's sufficient to track the changes of data
flavors available on the clipboard.
The following new API are to be introduced in the package
a listener interface FlavorListener,
an event FlavorEvent that is delivered to the
FlavorListeners whenever clipboard flavors change,
and the following Clipboard methods:
void Clipboard.addFlavorListener(FlavorListener listener),
void Clipboard.removeFlavorListener(FlavorListener listener),
Submitted On 18-JUN-2003
In what release would you anticipate the above API
(FlavorListener, FlavorEvent) being added ?
Submitted On 02-JUL-2003
As we can see it on this page:
Release Fixed tiger - JDK 1.5
Submitted On 18-JUL-2003
Yes - though on June 18 it was not listed as such.
Submitted On 19-OCT-2003
By my understanding (from post 2003-01-18) the FlavorEvent
will only fire if the flavour of the clipboard contents changes.
What would then happen if the user copied, say, a HTML
page, and then copied *another* HTML page?
Workaround: Well, I guess we could grab the clipboard
content, convert it to plain text, and throw it back... and
then when the second page is copied, the flavour changes.
Just something to think about...
Submitted On 21-OCT-2003
The approach (to notify about changes of data flavors
available on the clipboard) is sufficient to implement the
functionality mentioned in the description (enabling/disabling
paste option in a Java application depending on what flavors
clipboard data are available in). I guess, few applications
really need to be notified about all settings of new clipboard
contents (the only example I know is the Windows clipboard
Submitted On 05-NOV-2005
I plan to implement a small dictionary tool to translate botanic scientific names to native language names. The user may copy names from some application to the system clipboard. The tool observes the clipboard and displays the translation. For this a notification of every clipboard content change would be fine. The system clipboard implementation of Tiger (1.5) doesn't notify FlavorListeners reliable about content changes, see http://forums.java.sun.com/thread.jspa?threadID=679063.
Submitted On 07-NOV-2005
Sent are notifications about changes of data flavors
available on the clipboard. One should file an RFE if
they want API for notifications about all changes of
clipboard contents (e.g. some text is replaced with
other text). Anyway, one is able to implement the
required functionality now: retrieve text data from
the clipboard on a separate thread frequently.
PLEASE NOTE: JDK6 is formerly known as Project Mustang