Hi Robert,


On Thu, May 14, 1998 at 09:53:24PM -0700, Robert Lynch wrote:
> <snip>
>
> Say I highlight a couple of words in your e-mail and then use the paste
> button in Steve's java application (with your mods), then I get:
> -----
> Exception occurred during event dispatching:
> java.lang.NullPointerException:
>         at
> sun.awt.motif.X11Selection.isDataFlavorSupported(X11Selection.java:223)
>         at clipboard$2.actionPerformed(clipboard.java:53)
>         at java.awt.Button.processActionEvent(Button.java:257)
>         at java.awt.Button.processEvent(Button.java:230)
>         at java.awt.Component.dispatchEventImpl(Component.java:1781)
>         at java.awt.Component.dispatchEvent(Component.java:1708)
>         at java.awt.EventDispatchThread.run(EventDispatchThread.java:81)
> ----
> i.e., more or less what Steve was getting.

Yes, I get this too, *if* I use "paste" before I ever used "copy".

In this case the app receives an (almost) empty object:-

 AWT-EventQueue-0[1] dump data
 data = (sun.awt.motif.X11Selection)0x406e4ca8 {
     int atom = 161
     X11SelectionHolder holder = null
     Transferable contents = null
     String dataString = null
     int targetArray[] = null
     Object data = null
 }

After pressing copy (so, invoking Clipboard.setContents()), an object containing
a String is obtained:

 AWT-EventQueue-0[1] dump data
 data = (Transfer)0x406ec170 {
     String s = null
 }

This string is either null or whatever was highlighted *inside the application*
when copy was pressed.

>
> Whereas, if one makes the change to (gutting of!) his button method as I
> mentioned:
> -----
> ...
>         b2.addActionListener( new ActionListener() {
>             public void actionPerformed(ActionEvent e)
>             {
>
>               Transferable data=Clip.getContents(this);
>               //System.out.println("data is: "+data);
>                 String result = "";
>                 try {
>                     result =
> (String)data.getTransferData(DataFlavor.stringFlavor);
>                 }
>                 catch (Exception ex) {;}
>                 t2.setText (result);
>                 t1.setText (result);
>             }
>         });
> ...
> -------

Not for me! I don't see data from the system clipboard, even after making the
above change. And with the 2 lines I changed earlier, it still copies and pastes
data that is highlighted in the app.

The book (Javanut2) says you get system clipboard data from
Toolkit.getSystemClipboard() and that Clipboard() is for intra-application cut &
paste. That is the behaviour I am seeing with or without your changes.

Without your changes however, I do get the original error if paste is
"accidentally" done before copy.

> Matters are a bit confused here, IMO because there's not a lot of
> agreement on what's being attempted. ;-)

I'LL SAY

>
> FWIW.
>
> Bob L.

Cheers ... Duncan.
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);
        //System.out.println("data is: "+data);
        String result = "";
        try {
          result = (String)data.getTransferData(DataFlavor.stringFlavor);
        }
        catch (Exception ex) {;}
        t2.setText (result);
        t1.setText (result);
      }
    });

    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.getMimeType().equals("text/plain; charset=unicode")) 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;
  }

}

Reply via email to