TL;DR: I want to detect when content has finished transferring to the system clipboard.
Scenario
I am using a FlavorListener
to toggle a button when the clipboard content type changes. The listener is calling to Clipboard#getAvailableDataFlavors
, which will inconsistently throw an IllegalStateException
, when I copy data from other programs, saying, “cannot open system clipboard.”
MVCE
To reproduce the problem, run the program below and copy data to your system clipboard from another program (you may need multiple copy attempts before the exception is thrown):
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.FlavorListener;
import java.util.Arrays;
import java.util.Scanner;
public class Foo {
public static void main(String[] args) {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
FlavorListener listener = e -> System.out.print("rClipboard has text: "
+ Arrays.stream(clipboard.getAvailableDataFlavors()).anyMatch(DataFlavor::isFlavorTextType)
+ "nPress enter to exit...");
clipboard.addFlavorListener(listener);
System.out.print("Press enter to exit...");
new Scanner(System.in).nextLine();
}
}
Findings
I found these similar questions when researching this:
- In Java why do i get java.lang.IllegalStateException: cannot open system clipboard
- listen to clipboard changes, check ownership?
In both scenarios, the solution (which came from a Coderanch thread) was to use Thread#sleep(long)
to allow time for the new content to be transferred to the clipboard.
Objections
I prefer not rely on sleep
because there is no way of knowing with certainty when the content transfer will complete. Additionally:
- If you don’t sleep for long enough, the
IllegalStateException
will still occur. - A sleep duration which works for one machine is not guaranteed to work on a different machine.
- A sleep duration which one content transfer is not guaranteed to work for another transfer (e.g. copying a 720p image vs. copying a 4k image).
- If you “play it safe” use a very long sleep duration:
- The user may have to wait for the button to be enabled.
- The user may click the button before it is disabled (causing an error to occur).
Question
In any case, I would like to know if there is a listener or callback that can be registered to execute when a content has finished being transferring to the clipboard. Does anything like this exist?
To clarify, I open to the possibility of writing my own shared/dynamic link library if I need to utilize a native function to achieve this.
5