Why does not it run? (clipboard)

edited March 2020 in Questions about Code

Why does not it run? sorry I cannot write English well.

import java.awt.datatransfer.*;
import java.awt.Toolkit;
 
void setup() {
  setClipboardString("abc");//
  println(getClipboardString());
}

Answers

  • What happens when you try to run this? Does it compile? Does it throw a runtime Exception? Something else?

  • edited July 2014
    import java.awt.datatransfer.*;
    import java.awt.Toolkit;
    
      Final int abc = ("12345678")
      setClipboardString("abc");//
    

    Error・・・unexpected token:int

  • I'm really not sure what you're trying to do here. Please post the exact code- for example, the final keyword should not be capitalized. Your syntax is also off, as it's missing semicolons and I'm not sure why you're setting an int equal to a String. What exactly are you trying to do?

  • sorry. I do not understand about this web site. sorry

  • FluFlu
    edited July 2014

    Maybe something like this?

    import java.awt.datatransfer.*;
    import java.awt.Toolkit;
    
    final static int abc = 1234;
    String clipboardString = str(abc);
    
    setClipboardString(clipboardString);
    

    Untested code!

  • I have deleted the other discussion about this topic because it had no useful information and it duplicated the question posted here.

  • I ran my code and apparently it says that the function setClipboardString does not exist. I tried declaring the clipboard class like:

    import java.awt.datatransfer.*;
    import java.awt.Toolkit;
    
    Clipboard clipboard;
    
    final static int abc = 1234;
    String clipboardString = str(abc);
    
    clipboard.setClipboardString(clipboardString);
    

    But still no use. Are you sure of this function setClipboardString()? Where did you find the reference? Use Google Translate if you don't understand.

  • edited July 2014

    The clipboard can be used for different data types, the solution below will copy and paste text only, but it is a starting point.

    To use this code follow these 3 steps.

    STEP 1 Create a second tab in the Processing IDE called GClip.java - the spelling, and upper/lower case must match exactly GClip.java

    STEP 2 Copy this code into the GClip.java tab file.

    import java.awt.*;
    import java.awt.datatransfer.*;
    
    public class GClip implements ClipboardOwner {
    
      /**
       * Static reference to enforce singleton pattern
       */
      private static GClip gclip = null;
    
      /**
       * Class attribute to reference the programs clipboard
       */
      private Clipboard clipboard = null;
    
    
      /**
       * Copy a string to the clipboard
       * @param chars
       */
      public static boolean copy(String chars) {
        if (gclip == null)
          gclip = new GClip();
        return gclip.copyString(chars);
      }
    
      /**
       * Get a string from the clipboard
       * @return the string on the clipboard
       */
      public static String paste() {
        if (gclip == null)
          gclip = new GClip();
        return gclip.pasteString();
      }
    
      /**
       * Ctor is private so clipboard is only created when a copy or paste is 
       * attempted and one does not exist already.
       */
      private GClip() {
        if (clipboard == null) {
          makeClipboardObject();
        }
      }
    
      /**
       * If security permits use the system clipboard otherwise create 
       * our own application clipboard.
       */
      private void makeClipboardObject() {
        SecurityManager security = System.getSecurityManager();
        if (security != null) {
          try {
            security.checkSystemClipboardAccess();
            clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
          } 
          catch (SecurityException e) {
            clipboard = new Clipboard("Application Clipboard");
          }
        } else {
          try {
            clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
          } 
          catch (Exception e) {
          }
        }
      }
    
      /**
       * Copy a string to the clipboard. If the Clipboard has not been created
       * then create it.
       * @return true for a successful copy to clipboard
       */
      private boolean copyString(String chars) {
        if (clipboard == null)
          makeClipboardObject();
        if (clipboard != null) {
          StringSelection fieldContent = new StringSelection (chars);
          clipboard.setContents (fieldContent, this);
          return true;
        }
        return false;
      }
    
      /**
       * Gets a string from the clipboard. If there is no Clipboard
       * then create it.
       * @return if possible the string on the clipboard else an empty string
       */
      private String pasteString() {
        // If there is no clipboard then there is nothing to paste
        if (clipboard == null) {
          makeClipboardObject();
          return "";
        }
        // We have a clipboard so get the string if we can
        Transferable clipboardContent = clipboard.getContents(this);
    
        if ((clipboardContent != null) &&
          (clipboardContent.isDataFlavorSupported(DataFlavor.stringFlavor))) {
          try {
            String tempString;
            tempString = (String) clipboardContent.getTransferData(DataFlavor.stringFlavor);
            return tempString;
          }
          catch (Exception e) {
            e.printStackTrace ();
          }
        }
        return "";
      }
    
      /**
       * Reqd by ClipboardOwner interface
       */
      public void lostOwnership(Clipboard clipboard, Transferable contents) {
      }
    }
    

    STEP 3 Copy the following code into the main tab for your sketch. It is a very simple demo of how to use GClip to copy and paste text.

    String text = "Quark's clipper!";
    String copyOfText;
    
    // Copy to clipboard
    GClip.copy(text);
    println("Copied text = " + text);
    
    // Paste text into String
    copyOfText = GClip.paste();
    println("Pasted text = " + copyOfText);
    
Sign In or Register to comment.