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

Java 将HTML从JEditorPane复制到外部应用程序时出现问题,java,swing,openoffice.org,mozilla,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等。它的工作原理与预期相同,即我得到的纯文本带有HTML标记,或者根据应用程序需要的mimetyp

我无法将HTML从JEditorPane复制到系统剪贴板,然后粘贴到其他应用程序:

  • OpenOffice 3.2-表示“请求的剪贴板格式不可用”
  • Thunderbird 3.13-在粘贴上不执行任何操作
  • Firefox 3.6.9-接受纯文本,但在GMail中,“撰写邮件”在粘贴时不做任何操作
顺便说一下,我正在运行WinXP。在其他应用程序中,如文本编辑器、MS Outlook、MS Word等。它的工作原理与预期相同,即我得到的纯文本带有HTML标记,或者根据应用程序需要的mimetype格式化文本

有人知道怎么了吗?这是Swing中的问题还是OpenOffice/Mozilla中的问题

请参阅下面的测试应用程序并进行尝试。我也尝试过使用自定义的可转换文件,但一旦我使用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);
            }
        });
    }
}
导入java.awt.BorderLayout;
导入java.awt.FlowLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.JButton;
导入javax.swing.JComboBox;
导入javax.swing.JEditorPane;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.SwingUtilities;
/**
*演示JEditorPane和OpenOffice/Thunderbird/Firefox之间的复制/粘贴问题。
* 
*@作者马丁
*/
公共类HtmlCopyDemo扩展了JFrame
{
公共HtmlCopyDemo()
{
setDefaultCloseOperation(关闭时退出);
setLayout(新的BorderLayout());
设置大小(400400);
最终绝地窗格编辑器=新绝地窗格();
setContentType(“text/html”);
setText(“这里有一些格式化文本”);
添加(编辑器,BorderLayout.CENTER);
JPanel panel=newjpanel(newflowlayout());
添加(面板,边界布局。北);
JButton按钮=新JButton(“复制”);
面板。添加(按钮);
addActionListener(新建ActionListener())
{
@凌驾
已执行的公共无效操作(操作事件e)
{
editor.selectAll();
editor.copy();
}
});
最终JComboBox组合=新JComboBox(新对象[]{“text/html”,“text/plain”});
面板。添加(组合);
combo.addActionListener(新ActionListener()
{
@凌驾
已执行的公共无效操作(操作事件e)
{
String text=editor.getText();
setContentType((字符串)combo.getSelectedItem());
editor.setText(文本);
}
});
}
公共静态void main(字符串[]args)
{
SwingUtilities.invokeLater(新的Runnable()
{
@凌驾
公开募捐
{
新的HtmlCopyDemo().setVisible(true);
}
});
}
}

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

添加
Clipboard Clipboard=Toolkit.getDefaultToolkit().getSystemClipboard()到按钮的
操作执行
,我可以看到剪贴板上有正确的内容和完整的html:

<html>
  <head>

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

这里有一些格式化的文本

粘贴到Word 2007可以正常工作。

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

添加
Clipboard Clipboard=Toolkit.getDefaultToolkit().getSystemClipboard()到按钮的
操作执行
,我可以看到剪贴板上有正确的内容和完整的html:

<html>
  <head>

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

这里有一些格式化的文本

粘贴到Word 2007中可以正常工作。

是的,除了OOo/Mozilla之外,它在我测试的所有应用程序中都可以正常工作。我在Bugzilla上发现了这个bug报告,这可能是原因:是的,除了OOo/Mozilla之外,它在我测试的所有应用程序中都运行良好。我在Bugzilla发现了这个bug报告,这可能是原因: