将HTML从JEditorPane复制到外部应用程序时出现问题
我无法将HTML从JEditorPane复制到系统剪贴板,然后再粘贴到其他应用程序中:
I'm having trouble copying HTML from JEditorPane to system clipboard and then pasting into other applications:
- OpenOffice 3.2-说请求的剪贴板格式不可用"
- 雷鸟3.13-在粘贴时不执行任何操作
- Firefox 3.6.9-接受纯文本,但是例如在GMail中,撰写邮件"在粘贴时不起作用
顺便说一句,我正在运行WinXP.在其他应用程序(如文本编辑器,MS Outlook,MS Word等)中,它可以按预期工作,即,根据应用程序需要的模仿类型,我得到带有HTML标签的纯文本或带格式的文本.
I'm running WinXP by the way. In other applications like text-editors, MS Outlook, MS Word etc. it works as expected, ie I get plain text with HTML tags stripped or formatted text according to which mimetype the application wants.
任何人都知道怎么了?在Swing或OpenOffice/Mozilla中有问题吗?
Anyone has an idea what's wrong? Is it a problem in Swing or in OpenOffice/Mozilla?
请参阅下面的测试应用程序并尝试.我也尝试过使用自定义的 Transferable ,但是一旦我为 DataFlavor 提供了mimetype ="text/html",它就会在上述应用程序中停止工作.>
See test application below and try out. I've also tried with a custom Transferable but as soon as I provide a DataFlavor with mimetype="text/html" it stops to work in applications mentioned above.
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
/**
* Demonstrates problem with copy/paste between JEditorPane and OpenOffice/Thunderbird/Firefox.
*
* @author martin
*/
public class HtmlCopyDemo extends JFrame
{
public HtmlCopyDemo()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
setSize(400, 400);
final JEditorPane editor = new JEditorPane();
editor.setContentType("text/html");
editor.setText("<html><head></head><body>Here's some <b>formatted</b> <i>text</i></body></html>");
add(editor, BorderLayout.CENTER);
JPanel panel = new JPanel(new FlowLayout());
add(panel, BorderLayout.NORTH);
JButton button = new JButton("Copy");
panel.add(button);
button.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
editor.selectAll();
editor.copy();
}
});
final JComboBox combo = new JComboBox(new Object[]{"text/html", "text/plain"});
panel.add(combo);
combo.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
String text = editor.getText();
editor.setContentType((String) combo.getSelectedItem());
editor.setText(text);
}
});
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new HtmlCopyDemo().setVisible(true);
}
});
}
}
这很可能是接收方的问题. (因为我没有您的环境,所以我不能100%地确定.)
It's most likely a problem on the receiver end. (I can't be 100% sure since I don't have your environment.)
将Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
添加到按钮的actionPerformed
中,我可以看到剪贴板具有正确的HTML内容:
Add Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
to your button's actionPerformed
and I can see the clipboard has the right stuff with full html:
<html>
<head>
</head>
<body>
Here's some <b>formatted</b> <i>text</i>
</body>
</html>
正确粘贴到Word 2007中.
Pasting into Word 2007 works correctly.