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);
}
});
}
}
是的,它工作正常,除了OOo/Mozilla以外,我測試過的所有應用程序。我在Bugzilla發現了這個bug報告,這可能是原因:https://bugzilla.mozilla.org/show_bug.cgi?id = 395218 – Uhlen 2010-09-16 16:28:37