Results 1 to 14 of 14
  1. #1
    Fra
    Fra is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Question Best way to paste (from clipboard) to other windows in Windows

    Hi!

    I want to simple copy and paste text between my java program an another window. Example: Copy some text from java and paste it in Notepad. All stuff about getting foreground-background windows and copy to the clipboard it's done (I'm using JNA and Clipboard class), only what I need is to do paste (ctrl+v). Please tell me the best way to do that. Thanks.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Best way to paste (from clipboard) to other windows in Windows

    Why are you using JNA and Clipboard? JTextComponent has that functionality built in, along with appropriate Key Bindings. If you need programmatic access, there are public methods for cut/copy/paste.

    As far as pasting the text in another application is concerned, I don't know of any foolproof approach. One chancy way of doing it is by using java.awt.Robot.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Fra
    Fra is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: Best way to paste (from clipboard) to other windows in Windows

    Thanks for the reply DarrylBurke. I used JNA and Clipboard because what I want is to copy and paste to another program not written in java, like Notepad. Robot is for only java programs, or I'm wrong?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Best way to paste (from clipboard) to other windows in Windows

    You're wrong on both accounts.

    Have you gone through the API for JTextComponent and Robot?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Fra
    Fra is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: Best way to paste (from clipboard) to other windows in Windows

    Nop, I dismissed Robot because I read somewhere that it is not recommended for use in production environments and I viewed some Robot examples for testing only java programs. So is not true, isn't it?. Switching between different programs in windows can be done without using JNA or JNI?

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Best way to paste (from clipboard) to other windows in Windows

    Switching between windows is an entirely different kettle of fish, and something I've never tried. I guess that might be doable, in a system dependent manner, with JNI/JNA.

    You copy to the clipboard from your Java text component using its copy() method. Once you have switched windows you can instruct Robot to send a Ctrl+V to paste the content.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Best way to paste (from clipboard) to other windows in Windows

    Another approach might be to write out and execute a VBA Script. Note that the sleep time (500 ms) is arbitrary and is to allow Notepad to load fully. Also that the user can mess things up by bringing another window to the front before the paste takes place.
    Java Code:
    import java.io.File;
    import java.io.FileWriter;
    import javax.swing.JTextField;
    
    public class PasteToNotepad {
    
      public static void main(String[] args) throws Exception {
        String text = "Some text for testing.";
        JTextField textField = new JTextField(text);
        textField.setSelectionStart(0);
        textField.setSelectionEnd(text.length() - 1);
        textField.copy();
    
        String vbs = ""
                + "Set WshShell = WScript.CreateObject(\"WScript.Shell\")\n"
                + "WshShell.Run \"notepad\", 9\n"
                + "WScript.Sleep 500\n"
                + "WshShell.SendKeys \"^V\"";
    
        File file = File.createTempFile("PrintDialog", ".vbs");
        file.deleteOnExit();
        FileWriter fw = new java.io.FileWriter(file);
        fw.write(vbs);
        fw.close();
        Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
        p.waitFor();
      }
    }
    This doesn't help if your aim is to paste into an already running session of Notepad, possibly already containing text.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    Fra
    Fra is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: Best way to paste (from clipboard) to other windows in Windows

    Excellent, I just did a test using Robot and worked fine. In this case I can't use JTextComponent because the text comes from the code. So I still have to use JNA for managing windows, but no matter. Thanks db!!

  9. #9
    Fra
    Fra is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: Best way to paste (from clipboard) to other windows in Windows

    Thanks for the example!, it's a good idea too.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Best way to paste (from clipboard) to other windows in Windows

    Quote Originally Posted by Fra View Post
    In this case I can't use JTextComponent because the text comes from the code.
    So does the text in my VBA example ;)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  11. #11
    Fra
    Fra is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: Best way to paste (from clipboard) to other windows in Windows

    Yeap, this example can help me for other part of my program for opening new windows, but in this particulary case I must switch between windows already opened. Thanks again db.

  12. #12
    delete123 is offline Member
    Join Date
    Jul 2012
    Posts
    4
    Rep Power
    0

    Default Re: Best way to paste (from clipboard) to other windows in Windows

    Quote Originally Posted by DarrylBurke View Post
    Another approach might be to write out and execute a VBA Script. Note that the sleep time (500 ms) is arbitrary and is to allow Notepad to load fully. Also that the user can mess things up by bringing another window to the front before the paste takes place.
    Java Code:
    import java.io.File;
    import java.io.FileWriter;
    import javax.swing.JTextField;
    
    public class PasteToNotepad {
    
      public static void main(String[] args) throws Exception {
        String text = "Some text for testing.";
        JTextField textField = new JTextField(text);
        textField.setSelectionStart(0);
        textField.setSelectionEnd(text.length() - 1);
        textField.copy();
    
        String vbs = ""
                + "Set WshShell = WScript.CreateObject(\"WScript.Shell\")\n"
                + "WshShell.Run \"notepad\", 9\n"
                + "WScript.Sleep 500\n"
                + "WshShell.SendKeys \"^V\"";
    
        File file = File.createTempFile("PrintDialog", ".vbs");
        file.deleteOnExit();
        FileWriter fw = new java.io.FileWriter(file);
        fw.write(vbs);
        fw.close();
        Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
        p.waitFor();
      }
    }
    This doesn't help if your aim is to paste into an already running session of Notepad, possibly already containing text.

    db
    hey man, thanks that really helped me with the same purpose , but please help me, how can i change it to another program, i mean if i want to paste so in another program??
    i tried to put an directory path but it doesn't work, please respond as fast as you can... (-:
    Last edited by DarrylBurke; 07-20-2012 at 08:53 PM. Reason: Removed annoying oversize red bold formatting

  13. #13
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Re: Best way to paste (from clipboard) to other windows in Windows

    ^Please, open new thread and ask that question, don't hijack old threads.

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Best way to paste (from clipboard) to other windows in Windows

    1. Please don't post to old dead threads. When you have a question, start your own thread -- they're free. Feel free to link to any other related thread.

    2. Please keep technical questions on the forum. Private messaging will NOT get you a better or faster response. Nor will requesting an early response. Any urgency on your part is not our concern.

    3. Please don't misuse the formatting features in a misguided attempt to draw attention to your post. That will have the opposite effect, as entire paragraphs in large bold red font are more than likely to be perceived as annoying.

    I'm closing this thread. Feel free to ask your question, with adequate details, in a new thread. It might help you to get a better response if you first go through this web page: How to ask questions the smart way

    db

    THREAD CLOSED
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 5
    Last Post: 02-08-2012, 03:17 PM
  2. Windows java vs Windows AIX
    By sysp in forum New To Java
    Replies: 0
    Last Post: 01-26-2011, 06:32 AM
  3. How to copy and paste data with the clipboard
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-07-2008, 05:35 PM
  4. Replies: 1
    Last Post: 06-23-2008, 07:46 AM
  5. swt for windows
    By Gajesh Tripathi in forum SWT / JFace
    Replies: 2
    Last Post: 10-18-2007, 07:43 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •