java出现无法获取系统剪贴板,无法从Mac OS X上的后台Java应用程序监视系统剪贴板更改...

I have a java program, that runs in the background and monitors the system clipboard for changes (i do this through polling, as it seems to be the only way besides the "ownership-variant", where i have to reset the content all the time to become the owner). If it discovers an input text in an specific format, it processes that text and overwrites the clipboard with the result (so i can copy the input and right after it paste the result while the program is running in background).

This worked fine so far on Windows, but when running the same program on Mac OS X, the behavior is a little bit strange. As long as i don't copy my results into the system clipboard, the polling mechanism itself works as expected. But at the moment i set the clipboard content out of the java program the first time, it recognizes future extern changes only while becoming active. So i can't just let it run in the background, but instead i have to "copy input -> switch to java-program -> switch back -> paste result" all the time.

As that is annoying and thats exactly the thing i wanted to avoid by this "clipboard monitoring -> result pasting"-method, i would be very happy for any ideas how to fix that issue.

Edit: some code-fragements

public void setClipboardText(String text) {

if (text == null) {

throw new NullPointerException();

}

synchronized (this.lastFoundTextLock) {

this.lastFoundText = text;

Toolkit.getDefaultToolkit().getSystemClipboard()

.setContents(new StringSelection(text), null);

}

}

public String getClipboardText() {

Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().

getContents(null);

try {

if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {

String text = (String) t.getTransferData(DataFlavor.stringFlavor);

return text;

}

} catch (UnsupportedFlavorException e) {

} catch (IOException e) {

}

return null;

}

public void run() {

while (true) {

String currentClipboardText = getClipboardText();

boolean isNew;

synchronized (this.lastFoundTextLock) {

isNew = ((this.lastFoundText != null) || (currentClipboardText != null))

&& ((currentClipboardText == null) || !currentClipboardText

.equals(this.lastFoundText));

if (isNew) {

this.lastFoundText = currentClipboardText;

}

}

if (isNew && currentClipboardText != null) {

//new text found

fireNewClipboardTextFound(currentClipboardText);

}

try {

Thread.sleep(this.automaticCheckInterval);

} catch (InterruptedException e) {

// ignore

}

synchronized (this.monitorRunningLock) {

if (!this.monitorRunning) {

break;

}

}

}

}

解决方案

I see that several others have attempted what you're trying ( Can't copy to a clipboard from a background java application on MAC OSX ) and had marginal success ( Copying to Clipboard in Java ) and few good answers ( java/swing: clipboard paste ) but you might want to investigate further... Can anyone else comment on the changes in Java 6 wrt this issue?

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值