JDK-4067715 : Clipboard's getContents() problem
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.1.2
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_nt
  • CPU: x86
  • Submitted: 1997-07-28
  • Updated: 1999-12-14
  • Resolved: 1999-12-14
Related Reports
Duplicate :  
Description

Name: rlT66838			Date: 07/28/97


I'm running into the following problem trying to use the clipboard with
JDK 1.1.2
I greatly appreciate any suggestion and help that can solve this
blocking problem.

Maurizio Fabbri

1) I have developed a very simple class, named ClipObjectData:

    public class ClipObjectData implements java.io.Serializable {
      public long objectId;
      public byte objectType;
    
      public ClipObjectData(long id, byte type) {
          objectId = id;
          objectType = type;
      }
    }

2) Then I have developed a class implementing the Transferable
interface, responsible to handle a vector of ClipObjectData
instances and supporting just one data flavor:

  public class ObjectSelection implements Transferable {
                                        
      private Object selection = null;
      private DataFlavor flavor = null;

      public ObjectSelection(Object object,
                             DataFlavor dataFlavor) {
         selection = object;
         flavor = dataFlavor;
      }

      public DataFlavor [] getTransferDataFlavors() {
        DataFlavor [] dataFlavor = new DataFlavor[1];
        dataFlavor[0] = flavor;
        return dataFlavor;
      }

      public boolean isDataFlavorSupported(DataFlavor dataFlavor) {
        return flavor.equals(dataFlavor);
      }

      public Object getTransferData(DataFlavor dataFlavor)
                    throws UnsupportedFlavorException {
        if (isDataFlavorSupported(dataFlavor))
           return selection;
        else
           throw new UnsupportedFlavorException(dataFlavor);
      }

  }

3) Finally I have a class that is a ClipboardOwner which has the
   following methods:

   a) public void copyToClipboard() {
        DataFlavor flavor = new DataFlavor(Vector.class,
                                           "VectorOfClipObjectData");
        Vector selectedIds = new Vector();
        selectedIds.addElement(new ClipObjectData(1, (byte)10)); // just for testing
        ObjectSelection selection = new ObjectSelection(selectedIds,
                                                        flavor);
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(selection, this);
        return;
     }    

   b) public void pasteFromClipboard() {
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable clipdata = clipboard.getContents(this);
        if (clipdata != null) {
             try {
                   ....
                   ....
                 }
             catch (UnsupportedFlavorException e){
              ....
             }
        }
        .....

Now, when in pasteFromClipboard() I try to retrieve the clipboard
contents I don't get what I inserted in copyToClipboard().
Using the debugger I am able to check that the clipboard is actually
containing in its "contents" data member the 
ObjectSelection instance allocated in copyToClipboard(), but I get in
clipdata variable a reference to a StringSelection
object. I also noticed that the clipboard returned is not a 
java.awt.datatransfer.Clipboard instance but it is a
sun.awt.window.WClipboard instance that evidently overrides
the base class getContents() implementation.
How can I get the right clipboard contents?
Thank you very much.

company - IBM , email - ###@###.###
======================================================================

Comments
EVALUATION This bug is related to the bug #4066902 as suggested in the JDC comments.
11-06-2004

WORK AROUND Name: rlT66838 Date: 07/28/97 ======================================================================
11-06-2004

PUBLIC COMMENTS This bug is related to the bug #4066902 as suggested in the JDC comments.
10-06-2004