Java clipboardOwner Purpose?

2019-06-17 22:28发布

问题:

I wrote a Java application that copies a string to the system clipboard. The constructor uses

Clipboard.setContents(Transferable contents, ClipboardOwner owner)

http://docs.oracle.com/javase/6/docs/api/java/awt/datatransfer/Clipboard.html

I got it working fine but I am not sure what the ClipboardOwner does? Looking at the Java api does not really tell much info.

http://docs.oracle.com/javase/6/docs/api/java/awt/datatransfer/ClipboardOwner.html

Oddly enough even passing a ClippboardOwner = null works. So I'm not exactly sure what the point of it is? Does anyone have any idea?

回答1:

If your application, or one of it's components implements ClipboardOwner interface in appropriate way, it can show you that the user copied some data to the system clipboard from another application, or from another component of your own application. See this example.



回答2:

When the next person puts something into the clipboard the owner you give to the clipboard will be told that they are no longer on the clipboard. There is only one known implementation and it is empty according to this. So it looks like a vestigial tail that is just sort of hanging out.



回答3:

An example use-case is a terminal window implementation, where any selection gets automatically copied to the clipboard. The lostOwnership(..) callback may be used to unselect, so that the user knows that a selection is shown if and only if it is currently on the clipboard. This idiom is used e.g. in rxvt (which is not written in Java, however).