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

[9187 byte] By [Annettea] at [2007-10-3 10:14:44]
# 1

Your posted code works fine for me using JDK1.4.2 on XP.

Of course I had to remove:

import de.athena.text.html.*;

Maybe that is causing some problem.

By the way, the default layout for a frame and dialog is a BorderLayout. So the following line is not required:

this.setLayout(new BorderLayout());

camickra at 2007-7-15 5:35:17 > top of Java-index,Desktop,Core GUI APIs...
# 2

I haven't used Word in quite a while, but doesn't it let you choose what mime type to use when you copy from a document to the clipboard? If you copy the text as RTF, the JEditorPane will ignore it because it only looks for text in its own mime type (HTML, in your case) or plain text.

By the way the TransferHandler you're looking at is just a general-purpose base class; what JTextComponents use is javax.swing.plaf.basic.BasicTextUI.TextTransferHandler

Here's an article you might find useful: http://weblogs.java.net/blog/zixle/archive/2006/08/cut_copy_and_pa.html

uncle_alicea at 2007-7-15 5:35:17 > top of Java-index,Desktop,Core GUI APIs...