paste text from MS Word into JEditorPane
Hello!
I know there was a thread concerning the problem, that if I try to paste some text from MS word into JEditorPane that there might be some problems. But I cannot find it.
To explain the problem:
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import java.awt.*;
import java.util.*;
import de.athena.text.html.*;
publicclass HTMLTest_1extends JFrame{
/** Creates a new instance of HTMLTest */
public HTMLTest_1(){
JEditorPane p =new JEditorPane("text/html","");
p.setEditorKit(new HTMLEditorKit());
p.setEditable(true);
JScrollPane scroller =new JScrollPane();
JViewport port = scroller.getViewport();
port.add(p);
scroller.setPreferredSize(new Dimension(500, 400));
this.setLayout(new BorderLayout());
this.add(scroller, BorderLayout.CENTER);
}
publicstaticvoid main(String[] args){
HTMLTest_1 test =new HTMLTest_1();
test.pack();
test.setVisible(true);
}
}
Run this program an try to paste some text using Ctrl-V. It works, if the text comes from the notepad or something like that. If I try to paste text that I copied out of MS Word, nothing happens.
What can I do just to paste the text, I do not mind if the formatting gets lost. I tried to understand the source code. So where do I have to make some changes?
There is:
publicstaticclass PasteActionextends TextAction{
/** Create this object with the appropriate identifier. */
public PasteAction(){
super(pasteAction);
}
/**
* The operation to perform when this action is triggered.
*
* @param e the action event
*/
publicvoid actionPerformed(ActionEvent e){
JTextComponent target = getTextComponent(e);
if (target !=null){
target.paste();
}
}
}
in class DefaultEditorKit.
and when I look in JTextComponent I see:
publicvoid paste(){
if (isEditable() && isEnabled()){
invokeAction("paste", TransferHandler.getPasteAction());
}
}
And then I find
staticclass TransferActionextends AbstractActionimplements UIResource{
TransferAction(String name){
super(name);
}
publicvoid actionPerformed(ActionEvent e){
Object src = e.getSource();
if (srcinstanceof JComponent){
JComponent c = (JComponent) src;
TransferHandler th = c.getTransferHandler();
Clipboard clipboard = getClipboard(c);
String name = (String) getValue(Action.NAME);
Transferable trans =null;
// any of these calls may throw IllegalStateException
try{
if ((clipboard !=null) && (th !=null) && (name !=null)){
if ("cut".equals(name)){
th.exportToClipboard(c, clipboard, MOVE);
}elseif ("copy".equals(name)){
th.exportToClipboard(c, clipboard, COPY);
}elseif ("paste".equals(name)){
trans = clipboard.getContents(null);
}
}
}catch (IllegalStateException ise){
// clipboard was unavailable
UIManager.getLookAndFeel().provideErrorFeedback(c);
return;
}
// this is a paste action, import data into the component
if (trans !=null){
th.importData(c, trans);
}
}
}
So I look at the method importData in TransferHandler
publicboolean importData(JComponent comp, Transferable t){
PropertyDescriptor prop = getPropertyDescriptor(comp);
if (prop !=null){
Method writer = prop.getWriteMethod();
if (writer ==null){
// read-only property. ignore
returnfalse;
}
Class[] params = writer.getParameterTypes();
if (params.length != 1){
// zero or more than one argument, ignore
returnfalse;
}
DataFlavor flavor = getPropertyDataFlavor(params[0], t.getTransferDataFlavors());
if (flavor !=null){
try{
Object value = t.getTransferData(flavor);
Object[] args ={ value};
MethodUtil.invoke(writer, comp, args);
returntrue;
}catch (Exception ex){
System.err.println("Invocation failed");
// invocation code
}
}
}
returnfalse;
}
And here I get lost......
So what can I do to force the pasting of text? No matter where it comes from or if the formatting gets lost? I just want ANYTHING than nothing to happen......
Or is it not so complicated? I do not know anything about those DataTransfer-Stuff.......
Thank you very much! I really appreciate you all!
Annette

