将 HTML 从 JEditorPane 复制到外部应用程序时出现问题
我在将 HTML 从 JEditorPane 复制到系统剪贴板然后粘贴到其他应用程序时遇到问题:
- OpenOffice 3.2 - 说“请求的剪贴板格式不可用”
- Thunderbird 3.13 - 粘贴时不执行任何操作
- Firefox 3.6.9 - 接受纯文本,但例如在 GMail 中,“撰写邮件”对粘贴没有任何作用,
顺便说一句,我正在运行 WinXP。在文本编辑器、MS Outlook、MS Word 等其他应用程序中,它按预期工作,即我得到带有 HTML 标签的纯文本或根据应用程序所需的 mimetype 格式化的文本。
任何人都知道出了什么问题吗?这是 Swing 或 OpenOffice/Mozilla 中的问题吗?
请参阅下面的测试应用程序并尝试。我也尝试过使用自定义的 Transferable,但是一旦我提供带有 mimetype="text/html" 的 DataFlavor,它就会停止在上述应用程序中工作。
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);
}
});
}
}
I'm having trouble copying HTML from JEditorPane to system clipboard and then pasting into other applications:
- OpenOffice 3.2 - Says "Requested clipboard format isn't available"
- Thunderbird 3.13 - Does nothing on paste
- Firefox 3.6.9 - Accepts plain text but for example in GMail "Compose mail" does nothing on paste
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.
Anyone has an idea what's wrong? Is it a problem in Swing or in OpenOffice/Mozilla?
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);
}
});
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这很可能是接收端的问题。 (我不能 100% 确定,因为我没有您的环境。)
将
Clipboard Clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
添加到按钮的actionPerformed
我可以看到剪贴板有正确的内容和完整的 html:粘贴到 Word 2007 中可以正常工作。
It's most likely a problem on the receiver end. (I can't be 100% sure since I don't have your environment.)
Add
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
to your button'sactionPerformed
and I can see the clipboard has the right stuff with full html:Pasting into Word 2007 works correctly.