2010-09-16 46 views
2

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

  • 的OpenOffice 3.2 - 说“请求的剪贴板格式是不可用”
  • 雷鸟3.13 - 不执行任何操作上的粘贴
  • 火狐3.6.9 - 接受纯文本,但例如在GMail的“撰写邮件”不执行任何操作上的粘贴

我る顺便提一下WinXP。在文本编辑器,MS Outlook,MS Word等其他应用程序中,它可以按预期工作,也就是说,我可以根据应用程序需要的mimetype获取带有HTML标签的纯文本或剥离或格式化文本。

任何人有一个想法是什么错?这是Swing还是OpenOffice/Mozilla的问题?

请参阅下面的测试应用程序并尝试。我也试过用自定义的Transferable,但只要我提供了一个DataFlavor mimetype =“text/html”,它就停止在上面提到的应用程序中工作。

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); 
      } 
     }); 
    } 
} 

回答

0

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

+0

是的,它工作正常,除了OOo/Mozilla以外,我测试过的所有应用程序。我在Bugzilla发现了这个bug报告,这可能是原因:https://bugzilla.mozilla.org/show_bug.cgi?id = 395218 – Uhlen 2010-09-16 16:28:37