java - 将 HTML 从 JEditorPane 复制到外部应用程序时出现问题

标签 java swing openoffice.org mozilla jeditorpane

我在将 HTML 从 JEditorPane 复制到系统剪贴板然后粘贴到其他应用程序时遇到问题:

  • OpenOffice 3.2 - 显示“请求的剪贴板格式不可用”
  • Thunderbird 3.13 - 粘贴时不执行任何操作
  • Firefox 3.6.9 - 接受纯文本,但例如在 GMail 中,“撰写邮件”对粘贴不执行任何操作

顺便说一句,我正在运行 WinXP。在文本编辑器、MS Outlook、MS Word 等其他应用程序中,它按预期工作,即根据应用程序需要的 mimetype,我得到带有 HTML 标签剥离或格式化文本的纯文本。

谁知道哪里出了问题?是 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);
            }
        });
    }
}

最佳答案

这很可能是接收端的问题。 (我不能 100% 确定,因为我没有您的环境。)

Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); 添加到按钮的 actionPerformed 中,我可以看到剪贴板中包含正确的内容和完整的 html:

<html>
  <head>

  </head>
  <body>
    Here's some <b>formatted</b> <i>text</i>
  </body>
</html>

粘贴到 Word 2007 中工作正常。

关于java - 将 HTML 从 JEditorPane 复制到外部应用程序时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3727271/

相关文章:

java - 内部服务器错误,正在以第二所有者身份上传 Google App Engine 项目

java - 无法在 JFrame 上绘画

c# - 启动 openoffice calc 并打开 csv 文件

python - 使用适用于 Windows 的 pyUno 进行 OpenOffice.org 开发——哪个 Python?

java - 如何使用 java 将图像插入到 openoffice writer 文档中?

java - JPA使用set的哪个实现类

java - 什么时候将 Activity 传递给另一个类会导致内存泄漏?

java - 类包装器和 Java 与 Objective-C 中的单例有什么区别?

Java - 使用Thread而不是SwingWorker

java - 退出按钮覆盖整个屏幕