我想将文本从JTable单元格复制到剪贴板,使其可以粘贴到其他程序,如Microsoft Word.我有来自的文本JTable,但我不确定如何将其复制到剪贴板.
独立应用程序中的以下代码适用于ubuntu:
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
public class ClipboardTest {
public static void main(String[] args) throws Exception {
Clipboard clipBoard = Toolkit.getDefaultToolkit().getSystemClipboard();
// print the last copied thing
System.out.println(clipBoard.getContents(null).getTransferData(DataFlavor.stringFlavor));
StringSelection data = new StringSelection("NOW");
clipBoard.setContents(data, data);
// prints NOW
System.out.println(clipBoard.getContents(null).getTransferData(DataFlavor.stringFlavor));
}
}
Run Code Online (Sandbox Code Playgroud)
将(Ctrl + V)粘贴到不同的应用程序中不会产生任何结果; 我期待"现在".再次调用上面的代码会产生以下异常:
Exception in thread "main" java.awt.datatransfer.UnsupportedFlavorException: Unicode String
at sun.awt.datatransfer.ClipboardTransferable.getTransferData(ClipboardTransferable.java:160)
Run Code Online (Sandbox Code Playgroud)
作为一个独立的应用程序,这应该工作,即使经过2011安全更改.从JTextField内部通过Ctrl + C进行复制,然后粘贴到其他地方.
使用最新的java7(jdk1.7.0_10)和jdk1.6.0_33在ubuntu 11.04上都没有成功; 它应该工作,并在Windows 7上使用最新的java7和使用java6_37的mac osx 10.6上按预期工作.还尝试了xubuntu 12.04与那些javas,它在那里不起作用.这是一个linux/ubuntu错误吗?
JTextPane text;
text.setText("somewords <img src=\"file:///C:/filepath/fire.png\" text=\"[fire1]\" title=\"[fire2]\" alt=\"[fire3]\" style=\"width:11px;height:11px;\"> otherwords");
Run Code Online (Sandbox Code Playgroud)
给我这个
,这是预期的.但是当我突出显示它并复制粘贴它时,我会得到"somewords otherwords".在复制时在Firefox中完成的同样的事情将粘贴"somewords [fire3] otherwords"(它替代alt文本用于图像).有没有办法在复制alt文本时复制此行为,或者复制图片的任何其他指示?我猜它不是内置功能,所以我可能需要知道的是应该重载以模仿这种行为.
它用于输出/聊天窗口,因此重要的是当用户引用它时它包含图像(如表情)
更新:成功覆盖copyAction方法......现在怎么办?
// (should) allow copying of alt text in place of images
class CustomEditorKit extends HTMLEditorKit {
Action[] modifiedactions;
CustomEditorKit() {
int whereat=-1;
modifiedactions=super.getActions();
for(int k=0;k<super.getActions().length;k++) {
if(super.getActions()[k] instanceof CopyAction) //find where they keep the copyaction
{
whereat=k;
modifiedactions[whereat]=new CustomCopyAction(); //and replace it with a different one
}
}
}
@Override
public Action[] getActions() {
return modifiedactions; //returns the modified version instead of defaultActions …Run Code Online (Sandbox Code Playgroud) 我有一个非常简单的代码,它从类MyJFrame创建一个框架对象,该框架对象接受用作标题的第一个字符串。放置第二个字符串是要在JScrollPane中显示的文本。您可以在下面看到代码。我需要使用突出显示的文本进行复制和粘贴。我需要帮助实施它。因此,如果从菜单栏中选择了复制,它将复制突出显示的部分,如果粘贴则将其粘贴。
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.Container;
import javax.swing.JOptionPane;
public class DisplayText
{
private static JTextArea text;
public DisplayText(String title, String info)
{
MyJFrame f = new MyJFrame(title);
Container c = f.getContentPane();
//default text
text = new JTextArea(info);
//Scrollpane
JScrollPane sp = new JScrollPane(text);
c.add( sp );
f.setBounds(100,200, 500, 400 );
f.setVisible(true);
}
Run Code Online (Sandbox Code Playgroud) 有没有办法通过Scala将字符串复制到剪贴板?
与Python类似:
from Tkinter import Tk
r = Tk()
r.withdraw()
r.clipboard_clear()
r.clipboard_append('this is my text')
r.destroy()
Run Code Online (Sandbox Code Playgroud)
这个问题介绍了如何在Swing中将字符串复制到剪贴板,但我需要一些可以与Scala一起使用的东西: 在Java中复制到剪贴板