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

发布于 2024-09-19 03:53:43 字数 2594 浏览 10 评论 0原文

我在将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听闻余生 2024-09-26 03:53:43

这很可能是接收端的问题。 (我不能 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 中可以正常工作。

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'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>

Pasting into Word 2007 works correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文