The application now works for me after changing 2 lines. At least, after Copy of
slected text in pane 1 and "Paste", both panes display the text and nothing else
- was that the intention?
Using jdb, I found that the mime-type of the message was "text/plain;
charset=unicode", which Transfer's getTransferData method didn't recognise,
therefore returning null.
The second problem was that once getTransferData returned a non-null object, the
cast of it to StringReader caused a runtime exception. A cast to String is OK
however.
The changes are appended. Why it worked as-is on the other platforms is another
question.
-Duncan
On Wed, May 13, 1998 at 04:36:24PM +0100, Steve Zara wrote:
> This example code works fine on Solaris and Win95/NT:
>
>
> import java.net.*;
> import java.awt.*;
> import java.awt.datatransfer.*;
> import java.io.*;
> import java.awt.event.*;
>
> class clipboard extends Frame implements ClipboardOwner{
> TextArea t1=new TextArea("Area 1...",20,20);
> TextArea t2=new TextArea("Area 2...",20,20);
> Button b1=new Button("Copy");
> Button b2=new Button("Paste");
> Panel pA=new Panel();
> Panel pB=new Panel();
> ClipboardOwner owner=this;
> Clipboard Clip=getToolkit ().getSystemClipboard ();
>
> public void lostOwnership(Clipboard b,Transferable c)
> {
> System.out.println("We lost ownership!");
> }
>
> public static void main(String args[])
> {
> clipboard c=new clipboard();
> c.resize(new Dimension(300,300));
> c.show();
> }
>
> clipboard()
> {
> setLayout(new BorderLayout(10,10));
> pA.setLayout(new FlowLayout());
> pB.setLayout(new FlowLayout());
> pA.add("West",t1);
> pA.add("East",t2);
>
> b1.addActionListener( new ActionListener() {
>
> public void actionPerformed(ActionEvent e)
> {
> Transfer trans=new Transfer(t1.getSelectedText());
> Clip.setContents(trans,owner);
> }
> });
>
>
> b2.addActionListener( new ActionListener() {
> public void actionPerformed(ActionEvent e)
> {
>
> Transferable data=Clip.getContents(this);
> StringReader s;
> if (data.isDataFlavorSupported(new
>DataFlavor("text/insidejava","Test on InsideJava")))
> System.out.println("Transferable supports
>text/insidejava Mime Type.");
>
> if
>(data.isDataFlavorSupported(DataFlavor.stringFlavor))
> System.out.println("Transferable supports
>application/x-java-serializedobject Mime Type");
> try {
>
> String result = "";
>
>s=(StringReader)data.getTransferData(DataFlavor.plainTextFlavor);
>
> try {
> for (;;) {
> int c = s.read ();
> if (c == -1)
> break;
>
> result += (char) c;
> }
> } catch (Exception ioe) {
> }
> t2.setText (result);
> t1.setText (result);
>
>
> }
> catch (UnsupportedFlavorException ex)
>{System.out.println("Unsupported Flavor!");}
> catch (IOException ex)
>{System.out.println("IOException!");}
>
> }
> });
>
> pB.add(b1);
> pB.add(b2);
>
> add("South",pB);
> add("Center",pA);
> }
> }
>
>
>
> class Transfer implements Transferable
> {
> String s;
>
> public Transfer(String s)
> {
> this.s=s;
> }
>
> public Object getTransferData(DataFlavor flavor) throws
>UnsupportedFlavorException, IOException
> {
> if (flavor.getMimeType().equals("text/insidejava")) return s;
> if (flavor==DataFlavor.stringFlavor) return s;
> return null;
> }
>
> public boolean isDataFlavorSupported(DataFlavor flavor)
> {
> System.out.println("isDataFlavorSupported Method called with:");
> System.out.println(" Mime Type: "+flavor.getMimeType());
> System.out.println(" Human Name: "+flavor.getHumanPresentableName());
> if (flavor.getMimeType().equals("text/insidejava")
>||flavor==DataFlavor.stringFlavor) return true;
> else return false;
> }
>
> public DataFlavor[] getTransferDataFlavors()
> {
> DataFlavor d[]=new DataFlavor[1];
> d[0]=new DataFlavor("text/insidejava","Test on InsideJava");
> d[1]=DataFlavor.stringFlavor;
> return d;
> }
>
> }
>
> (This is a severe modification of someone else's example)
>
> The stack trace is:
> Exception occurred during event dispatching:
> java.lang.NullPointerException:
> at sun.awt.motif.X11Selection.isDataFlavorSupported(X11Selection.java:223)
> at clipboard$2.actionPerformed(Main.java:53)
> at java.awt.Button.processActionEvent(Button.java:254)
> at java.awt.Button.processEvent(Button.java:227)
> at java.awt.Component.dispatchEventImpl(Component.java:1764)
> at java.awt.Component.dispatchEvent(Component.java:1704)
> at java.awt.EventDispatchThread.run(EventDispatchThread.java:63)
>
>
> I want to complete an application that can accept text from the
> clipboard, so this is a crucial bug for me.
>
> Also, can I join the mailing list?
>
>
*** clipboard.java 1998/05/14 06:17:41 1.1
--- clipboard.java 1998/05/15 02:50:04
***************
*** 58,64 ****
try {
String result = "";
! s=(StringReader)data.getTransferData(DataFlavor.plainTextFlavor);
try {
for (;;) {
--- 58,64 ----
try {
String result = "";
! s=new
StringReader((String)data.getTransferData(DataFlavor.plainTextFlavor));
try {
for (;;) {
***************
*** 103,108 ****
--- 103,109 ----
public Object getTransferData(DataFlavor flavor) throws
UnsupportedFlavorException, IOException
{
if (flavor.getMimeType().equals("text/insidejava")) return s;
+ if (flavor.getMimeType().equals("text/plain; charset=unicode")) return s;
if (flavor==DataFlavor.stringFlavor) return s;
return null;
}