gpt4 book ai didi

java - setCaretPosition 引发无法打开系统剪贴板异常

转载 作者:行者123 更新时间:2023-12-01 23:29:02 24 4
gpt4 key购买 nike

我的 Swing 应用程序中有 JEditorPane (communicationView),有时我需要将其滚动到底部。滚动是通过将插入符位置设置到文档末尾来完成的。

private void setCommunicationViewCaretPosition(){

communicationView.setCaretPosition(communicationView.getDocument().getLength());

}

这通常有效,但一段时间后会抛出以下异常:

Exception in thread "Thread-6" java.lang.IllegalStateException: cannot open system clipboard
at sun.awt.windows.WClipboard.openClipboard(Native Method)
at sun.awt.datatransfer.SunClipboard.getClipboardFormatsOpenClose(SunClipboard.java:332)
at sun.awt.datatransfer.SunClipboard.isDataFlavorAvailable(SunClipboard.java:192)
at org.jdesktop.application.TextActions.updateTextActions(TextActions.java:132)
at org.jdesktop.application.TextActions.access$400(TextActions.java:47)
at org.jdesktop.application.TextActions$TextComponentCaretListener.caretUpdate(TextActions.java:115)
at javax.swing.text.JTextComponent.fireCaretUpdate(JTextComponent.java:407)
at javax.swing.text.JTextComponent$MutableCaretEvent.fire(JTextComponent.java:4417)
at javax.swing.text.JTextComponent$MutableCaretEvent.stateChanged(JTextComponent.java:4439)
at javax.swing.text.DefaultCaret.fireStateChanged(DefaultCaret.java:802)
at javax.swing.text.DefaultCaret.changeCaretPosition(DefaultCaret.java:1277)
at javax.swing.text.DefaultCaret.handleSetDot(DefaultCaret.java:1173)
at javax.swing.text.DefaultCaret.setDot(DefaultCaret.java:1154)
at javax.swing.text.DefaultCaret.setDot(DefaultCaret.java:1051)
at javax.swing.text.JTextComponent.setCaretPosition(JTextComponent.java:1680)
at aau.application.ApplicationView.setCommunicationViewCaretPosition(ApplicationView.java:643)

我不明白为什么会发生这种情况,或者设置插入符位置与系统剪贴板有什么关系。

设置插入符位置似乎是向下 ScrollView 的最佳方法,但任何其他方法都可以解决我的问题。

最佳答案

访问剪贴板:

使用此文本创建一个类文件

import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.Toolkit;
import java.io.*;

public final class clipboardData implements ClipboardOwner {

public static void main(String... aArguments ){
clipboardData Clipboard = new clipboardData();
}

/**
* Empty implementation of the ClipboardOwner interface.
* @param aClipboard
* @param aContents
*/
@Override
public void lostOwnership(Clipboard aClipboard, Transferable aContents){
//do nothing
}

/**
* Place a String on the clipboard, and make this class the
* owner of the Clipboard's contents.
* @param aString
*/
public void setData(String aString){
StringSelection stringSelection = new StringSelection(aString);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, this);
}

/**
* Get the String residing on the clipboard.
*
* @return any text found on the Clipboard; if none found, return an
* empty String.
*/
public String getData() {
String result = "";
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
//odd: the Object param of getContents is not currently used
Transferable contents = clipboard.getContents(null);
boolean hasTransferableText =
(contents != null) &&
contents.isDataFlavorSupported(DataFlavor.stringFlavor);
if (hasTransferableText) {
try {
result = (String)contents.getTransferData(DataFlavor.stringFlavor);
}
catch (UnsupportedFlavorException | IOException ex){
System.out.println(ex);
ex.printStackTrace();
}
}
return result;
}
}

上面的代码可以通过以下函数访问:

创建一个新的剪贴板编辑器:

clipboardData clipboardData = new clipboardData();

setData函数:

clipboardData.setData(Data);

获取数据函数:

clipboardData.getData();

关于java - setCaretPosition 引发无法打开系统剪贴板异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19611805/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com