This site requires JavaScript, please enable it in your browser!
Greenfoot back
finnland
finnland wrote ...

2012/12/22

How to copy a given text to clipboard??

finnland finnland

2012/12/22

#
I want to have a method wich copys a text into the clipboard. So when I use SRTG+V I can paste it into an Texteditor (Word, Editor ...). Would be greate if you know how ;)
vonmeth vonmeth

2012/12/22

#
danpost danpost

2012/12/22

#
@vonmeth, can you post the example here please. Or at least explain the basics of the code (what classes are used and how to access and write to the clipboard).
vonmeth vonmeth

2012/12/23

#
@danpost, I really could not explain it as I do not fully understand implementing interfaces (still learning), but I can implement (ha .. ha) the code into a working example from the code given in the link above. Wherever you wish to copy some text into the clipboard:
        TextTransfer textTransfer = new TextTransfer();
        textTransfer.setClipboardContents("blah, blah, blah");
Create a class with this code:
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.Toolkit;
import java.io.*;

public final class TextTransfer implements ClipboardOwner {

   /**
   * Empty implementation of the ClipboardOwner interface.
   */
   public void lostOwnership( Clipboard aClipboard, Transferable aContents) {
     //do nothing
   }

  /**
  * Place a String on the clipboard, and make this class the
  * owner of the Clipboard's contents.
  */
  public void setClipboardContents( String aString ){
    StringSelection stringSelection = new StringSelection( aString );
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    clipboard.setContents( stringSelection, this );
  }

  /**
  * Get the String residing on the clipboard.
  *
  * @return any text found on the Clipboard; if none found, return an
  * empty String.
  */
  public String getClipboardContents() {
    String result = "";
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    //odd: the Object param of getContents is not currently used
    Transferable contents = clipboard.getContents(null);
    boolean hasTransferableText =
      (contents != null) &&
      contents.isDataFlavorSupported(DataFlavor.stringFlavor)
    ;
    if ( hasTransferableText ) {
      try {
        result = (String)contents.getTransferData(DataFlavor.stringFlavor);
      }
      catch (UnsupportedFlavorException ex){
        //highly unlikely since we are using a standard DataFlavor
        System.out.println(ex);
        ex.printStackTrace();
      }
      catch (IOException ex) {
        System.out.println(ex);
        ex.printStackTrace();
      }
    }
    return result;
  }
} 
finnland finnland

2012/12/23

#
Thank you very much :) It works :D
finnland finnland

2013/1/18

#
Sadly it doesn't work on the Greenfoot website. :( Help please.
danpost danpost

2013/1/18

#
I do not think, and this may need confirmation from one of the Greenfoot team members, that Greenfoot allows access to any clipboard objects on the site (it certainly would not have access to a clipboard object on your system from the site).
davmac davmac

2013/1/19

#
Yes. It's Java that doesn't allow access to the clipboard to applets, for security reasons, not actually anything specifically to do with Greenfoot.
finnland finnland

2013/1/19

#
OK. This doesn't work on this page. But I need a way do to display a String, so the player can post it as a comment or send it to me via E-mail. Is there a way??
danpost danpost

2013/1/19

#
You could use the UserInfo class as a means to recieve a String up to 250 characters long (or more if you are clever enough). Because you created the scenario, you would have access to everyones UserInfo data, while everyone else could only write to their own and only have access to what you programmatically allow them. Alternatively, I have a Private Messaging scenario uploaded which would allow you to communicate one-on-one with other users. However, messages transmitted by it can only be up to 49 characters long; but that should be enough to pass email addresses and the like.
finnland finnland

2013/1/19

#
But it would be way more easy if i could display the String and the player would be possible for the player to mark and copy it. In Greenfoot it would be the consol were the error code is also shown. But on the website you can't see the consol. Or is there a way to show it?
vonmeth vonmeth

2013/1/19

#
import javax.swing.JOptionPane;
import javax.swing.JTextArea;

        String sb = "Hello text to be shown and copied here";
        JTextArea text = new JTextArea(sb);
        if(Greenfoot.isKeyDown("space"))
        JOptionPane.showMessageDialog(null, text, "Copy and paste", JOptionPane.INFORMATION_MESSAGE);
I believe that will work on the website as well.
finnland finnland

2013/1/20

#
Thanks. It works fine!!! (http://www.greenfoot.org/scenarios/6835 in the Editor)
You need to login to post a reply.