Determining When an Item Is No Longer on the System Clipboard
When an item is set on the system clipboard, it is possible to be
notified when that item is no longer on the clipboard. This is done
by including a clipboard owner object when setting the item.
Here's some code that uses the clipboard owner:
// This class serves as the clipboard owner.
class MyClipboardOwner implements ClipboardOwner {
// This method is called when this object is no longer
// the owner of the item on the system clipboard.
public void lostOwnership(Clipboard clipboard, Transferable contents) {
// To retrieve the contents, see
// Getting and Setting Text on the System Clipboard
}
}
// Create a clipboard owner
ClipboardOwner owner = new MyClipboardOwner();
// Set a string on the system clipboard and include the owner object
StringSelection ss = new StringSelection("A String");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, owner);
Post a comment