Search code examples
javaswingclipboardawtrobot

How to Copy multiple text line in clipboard and paste in another non java form


I want to copy multiple text line in System clipboard and paste them row by row into another application.

Example to copy:

a
b
c
d
e

Paste: a, b, c, ...

I then want to paste it in another non java program (this program contains a text box into which I want to paste the text.). It should still be in the same format even after pasting. After each paste, it should automatically press tab and move the focus to another text box. Rinse and repeat for 2 more rows of text.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    try {
        Robot r = new Robot();
    } catch (AWTException ex) {
        Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
    }

    String toClipBoardText = jTextField1.getText()+"\n"+jTextField2.getText()+"\n"+jTextField3.getText()+"\n"+jTextField4.getText()+"\n"+jTextField5.getText();
    StringSelection stringClip = new StringSelection(toClipBoardText);
    clip.setContents(stringClip, stringClip);
}  

Solution

  • We use a Scanner to traverse through the lines of code. Then we set the next line to the clipboard and press Ctrl + V to paste the data using the Robot class. After clicking your JButton you have 5 seconds to click into the wanted text box. It will then start pasting and tabbing.

    There are some sleep(...) statements in there because I don't know the UI you are working with it better save than sorry and give it some time to react.

    import java.awt.*;
    import java.awt.datatransfer.StringSelection;
    import java.awt.event.ActionEvent;
    import java.util.Scanner;
    
    import static java.awt.event.KeyEvent.*;
    
    // ...
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        try {
            String multiLineText = jTextField1.getText()+"\n"+jTextField2.getText()+"\n"+jTextField3.getText()+"\n"+jTextField4.getText()+"\n"+jTextField5.getText();
            Scanner textReader = new Scanner(multiLineText);
            Robot r = new Robot();
    
            System.out.println("You have 5 seconds to focus the text box into which the text will be pasted!");
    
            for (int i = 0; i < 5; i++) {
                System.out.println(5 - i + "...");
                Thread.sleep(1000);
            }
            System.out.println("Start pasting...");
            while (textReader.hasNext()) {
                String line = textReader.nextLine().trim();
                System.out.println("\t> Pasting \"" + line + "\"");
    
                Toolkit.getDefaultToolkit()
                        .getSystemClipboard()
                        .setContents(
                                new StringSelection(line),
                                null);
    
                pressKeys(r, VK_CONTROL, VK_V);
                pressKeys(r, VK_TAB);
            }
    
        } catch (AWTException | InterruptedException ex) {
            throw new RuntimeException(ex);
        }
    }
    
    public static void pressKeys(Robot robot, int... keys) throws InterruptedException {
        for (int i = 0; i < keys.length; i++) {
            robot.keyPress(keys[i]);
            Thread.sleep(10);
        }
        for (int i = 0; i < keys.length; i++) {
            robot.keyRelease(keys[keys.length - i - 1]);
            Thread.sleep(10);
        }
        Thread.sleep(100);
    }