Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: New to Java Help with Clipboard

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default New to Java Help with Clipboard

    I am trying to copy a text file using scanner to the clipboard. Can someone tell me what my clipboard.setContents method should look like?

    This is what I have now:

    import java.awt.Toolkit;
    import java.awt.datatransfer.Clipboard;
    import java.awt.datatransfer.StringSelection;
    import java.io.File;
    import java.util.Scanner;
     
    public class Main {
    public static void main(String[] args) {
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    File file = new File ("c:\\Temp\\testdata.txt");
     
    try{
    Scanner scanner = new Scanner(file);
     
    while (scanner.hasNextLine()) {
     
    String line = scanner.nextLine();
     
    System.out.println(line);
     
    }
     
    clipboard.setContents(new StringSelection("string"), null); //right now this just copies "string" to the clipboard. I need it to copy the scanned in file to the clipboard.
     
    } catch (Exception e) {}
    }
     
    }
    Last edited by copeg; May 10th, 2011 at 08:38 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,267
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: New to Java Help with Clipboard

    For future reference, please use the code tags.

    Break the problem down into what you need...1) read the file into memory (see The StringBuilder Class (The Java™ Tutorials > Learning the Java Language > Numbers and Strings) ). 2) pass the file contents as a String to the Clipbard as a StringSelection object (use value from 1) to do so).

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,267
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: New to Java Help with Clipboard

    Nice to know I spent my time trying to help only to have it wasted...at least have the courtesy to mark your thread as solved.
    This thread has been cross posted here:

    http://www.java-forums.org/new-java/43857-text-file-clipboard.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


Similar Threads

  1. Problem with Console Input from Clipboard
    By Nilhanth in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: March 1st, 2010, 05:47 PM