Results 1 to 6 of 6

Thread: Java code for copying text to the clipboard

  1. #1
    Join Date
    Mar 2010
    Posts
    222

    Java code for copying text to the clipboard

    Hello,
    I have a String variable with some text inside that I copy into the clipboard. How can I do this, i have tried this
    Code:
    Clipboard cl = getToolkit().getSystemClipboard();
    String msg = "soem text";
    cl.setContent(msg, msg);
    Of course this does not work but I do not understand how, if you have a good solution for the same then please post and help me. Thanks in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    347

    Re: Java code for copying text to the clipboard

    Hello,
    And what you get as an error? If you post back the error message you are getting then it can help us find a proper solution. A prior, the javadoc says that on clipboard takes an object "Transferable". However, your String does not implement this interface if I understand. Hence a method that you have the right to appeal.

  3. #3
    Join Date
    Nov 2009
    Posts
    335

    Re: Java code for copying text to the clipboard

    Hello,
    The answer is in the signature of your method. We must create a class that implements Transferable, implement the methods corresponding to this interface, and of course ensure that this object stores your string. Other method (because I'd be surprised that such a class does not exist): you look on Google or you guess which class meets your needs.

  4. #4
    Join Date
    Nov 2009
    Posts
    356

    Re: Java code for copying text to the clipboard

    Hello,
    I agree with the above post and i would like to add some more details to it.
    To guess you put a text in your clipboard (from any publisher). You retrieve the text using the Clipboard into a java program. You look at what the actual type of the object that you get. Learn to cope is important in computer, I hope you are understating the object oriented concepts here.

  5. #5
    Join Date
    Nov 2009
    Posts
    518

    Re: Java code for copying text to the clipboard

    Hello,
    To put a string in the clipboard you can do something like this, I think this is what you are looking for, this may not be as exact code that you will require, but some where near to it
    Here is the code
    Code:
    String mg = "pouet";
    StringSelection sers = new StringSelection(mg);
    Toolkit.getDefaultToolkit().getSystemClipboard().setContent(sers null);

  6. #6
    Join Date
    Nov 2009
    Posts
    335

    Re: Java code for copying text to the clipboard

    Hello,
    Just have a look at the following code, this might help you
    Code:
    String str = jt.getSelectedText();
       StringSelection strsel = new StringSelection(str);
       Clipboard clbrd = 
            Toolkit.getDefaultToolkit().getSystemClipboard();
       clbrd.setContents(strsel, strsel);
    This is not the whole code , but just a part of the code, I think this will help you. Thanks in advance.

Similar Threads

  1. Copying text from OCR scans
    By dwoehrle in forum Windows Software
    Replies: 1
    Last Post: 28-10-2011, 03:53 PM
  2. What is the limit of copying in Chrome clipboard
    By Nitnam in forum Technology & Internet
    Replies: 4
    Last Post: 18-06-2011, 12:47 AM
  3. Outlook 2010 crash while copying to clipboard
    By Ridzuan78 in forum Windows Software
    Replies: 4
    Last Post: 12-11-2010, 05:18 AM
  4. Replies: 4
    Last Post: 21-01-2010, 07:16 PM
  5. Replies: 4
    Last Post: 03-04-2009, 11:46 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,649,801.92006 seconds with 17 queries