Java Clipboard error (error)?

I have a program that opens a program, then copies and pastes a line into the program, and after a while, it copies the line from the program to the clipboard using the ctrl + c robot. My program then checks if the copied string contains a word, but instead of checking the newly copied string, it uses the previously copied start string. Here's some code:

new ProcessBuilder("pathToProgram").start();
copy(STRING1);
paste();
Thread.sleep(x);
//Move mouse to a position
//robot uses ctrl+a
copy();
Thread.sleep(100);
clipboardData = (String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor);
system.out.println(clipboardData);
if(clipboardData.contains(String2){
//do some stuff
}

      

The system.out value only outputs the String1 value, not the string2 value. Thanks for your help.

+1


source to share


1 answer


Assuming your copy () method sets the clipboard text, I have the same problem and found a strange workaround.

Java clipboard ignores user copy unless SwingUtilities.invokeLater ()



I can get a clipboard text that will always show whatever the user has copied there at any time. But if I programmatically create the clipboard text, then after that everything I get from the clipboard, except when I postpone other clipboard text, getting through SwingUtilities.invokeLater () once, then the clipboard text becomes fine again ... Then I can break it down again and "fix" it again.

I have no idea why this is the case, which is why the question I just posted (see link).

+1


source







All Articles