HOW TO SET A STRING TO CLIPBOARD DATA USING JAVA?

In this blog, I am going to explain about "How to set a string to clipboard data using java?". We will learn the automated way of copy-paste concept in this blog and later in future you can use this in any program.



Below is the example Java program to set a string to clipboard data and paste same in Notepad file.
Read more about HOW TO OPEN EXECUTABLE (.EXE) APPLICATION USING JAVA?
Read more about WHAT IS ROBOT CLASS IN JAVA?
Read more about WHAT IS TOOLKIT CLASS IN JAVA?



Java Source code:

package com.helloselenium.selenium.test;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.io.IOException;

public class CopyPaste{

  String string = "Hello Selenium"; 
  StringSelection stringSelection = new StringSelection(string);
  Toolkit.getDefaultToolkit().getSystemClipboard()
        .setContents(stringSelection, null);

  try {
     Runtime runtime = Runtime.getRuntime();
     runtime.exec("C:\\Windows\\System32\\Notepad.exe");
     
     Robot robot = new Robot();
     robot.keyPress(KeyEvent.VK_CONTROL);
     robot.keyPress(KeyEvent.VK_V); 
     robot.keyRelease(KeyEvent.VK_V); 
     robot.keyRelease(KeyEvent.VK_CONTROL); 

     } catch (AWTException e) {
        e.printStackTrace();
     } catch (IOException e1) {
        e1.printStackTrace();
   } 

}

Below are the line by line code explanation for above Java program:




package com.helloselenium.selenium.test;
Above code is package structure for current program file.
Following code is the required packages to handle Java AWTException.
import java.awt.AWTException;
Following code is the required packages for Java AWT Robot class.
import java.awt.Robot;
Following code is the required packages for Java AWT Toolkit class.
import java.awt.Toolkit;
Following code is the required packages for string selection.
import java.awt.datatransfer.StringSelection;
Following code is the required packages to Java AWT KeyEvent class.
import java.awt.event.KeyEvent;
Following code is the required packages to handle Java IOException.
import java.io.IOException;
Following code is to define string which we have to copy, you can replace this with your test string.
String string = "Hello Selenium"; 
Following code is to select the defined string.
StringSelection stringSelection = new StringSelection(string);
Following code is to set clipboard data for selected string.
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
Following code is to define the runtime object.
Runtime runtime = Runtime.getRuntime();
Following code is to open Notepad.exe file.
runtime.exec("C:\\Windows\\System32\\Notepad.exe");
Following code is to define new robot object.
Robot robot = new Robot();
Following code is to press CTRL key.
robot.keyPress(KeyEvent.VK_CONTROL);
Following code is to press V key.
robot.keyPress(KeyEvent.VK_V); 
Following code is to release CTRL key.
robot.keyRelease(KeyEvent.VK_V);
Following code is to release V key.
robot.keyRelease(KeyEvent.VK_CONTROL); 


Post a Comment

0 Comments