Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4067715

Clipboard's getContents() problem

XMLWordPrintable

    • x86
    • windows_nt



      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 - ###@###.###
      ======================================================================

            Unassigned Unassigned
            rlewis Roger Lewis (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: