java出现无法获取系统剪贴板,java剪贴板错误(错误)?

I have a program, which opens a program, then it copies and pastes a string into the program and after some time it copies a string from the program to the clipboard using a robot with ctrl+c. Then my program checks if the copied string contains a word, but instead of inspecting the recently copied string it uses the previous copied string of the beginning. Heres 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 thing outputs just the value of String1 not the value string2. Thanks for your help.

解决方案

Assuming that your copy() method sets the clipboard text, I am having the exact same problem and have found a weird workaround.

I can get the clipboard text, which will always show whatever the user has copied there at any time. But if I set the clipboard text programmatically, afterwards that's all I get from the clipboard - except if I postpone further clipboard text getting via SwingUtilities.invokeLater() one time, then the clipboard text getting works fine again. I can then also break it again - and "fix" it again.

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值