Skip to content
Related Articles
Open in App
Not now

Related Articles

How to Copy Text to the Clipboard in Java?

Improve Article
Save Article
Like Article
  • Last Updated : 17 Jan, 2023
Improve Article
Save Article
Like Article

We will be going through some source code for a Java method that lets you copy texts to the clipboard of your operating system. Java provides a class called Clipboard, which is found in transfer. Clipboard provides important functionalities of the Graphical User Interface(GUI), namely the COPY(CTRL+C), PASTE(CTRL+V), and CUT(CTRL+X).

java.awt.datatransfer: This package has been deprecated in Java 9 and later versions, in favor 
of the newer java.awt.dnd package for drag-and-drop support.
  • To get the reference of Clipboard  via Toolkit is: Toolkit toolkit=Toolkit.getDefaultToolkit();
  • To Obtain the Clipboard reference: Clipboard clipboard=Toolkit.getDefaultToolkit().getSystemClipboard();


Let us assume, we want to perform the copy operation (similar to pressing Ctrl+C) of a string value and paste it on Clipboard with the paste operation (Ctrl+V). To be clear we will copy the string without giving the command ctrl+C from the keyboard instead we will copy the string via Java Source Code.


The below steps will be followed to copy texts to the clipboard.

  1. OPEN ANY JAVA IDE, Here we will be using Spring Tool Suite(STS) Java IDE.
  2. GO TO FILE, After opening Java IDE.
  3. GO TO NEW i.e. press ALT+SHIFT+N
  4. CLICK ON Java Project


Give the Project Name and fill in other needed fields accordingly.


After filling in all the needed fields, click on Finish. Go to the src folder of the project that you have created. Right-click on the src file and create a package. Right-click on the package created above. Create a Java Class. The code is given below.


package First_Java_Project;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class copytoClipboard
    extends JFrame implements ActionListener {
    JTextField t1;
    JButton b1;
        t1 = new JTextField();
        t1.setBounds(20, 40, 400, 200);
        b1 = new JButton("COPY CONTENT!");
        b1.setBounds(70, 270, 150, 50);
        setBounds(200, 90, 500, 500);
    public static void main(String args[])
        new copytoClipboard();
    public void actionPerformed(ActionEvent e)
        String str = t1.getText();
        Clipboard clip = Toolkit.getDefaultToolkit()
        StringSelection strse1 = new StringSelection(str);
        clip.setContents(strse1, strse1);
                                      "TEXTS ARE COPIED!");
// This code is contributed by Rahul Chauhan.

After writing the code, it will show errors in the written source code. 


After importing and resolving all the errors, RUN the Java Project. Observe the events, A popup window will show. Write the content that you want to copy inside the appeared window.


Click on the Button COPY CONTENT! A message box appears narrating the message that TEXTS ARE COPIED!


Now go to Notepad or any other editing tool to test whether texts are copied are not. Use CTRL+V to paste it. Finally, we will be able to see that the written text inside the window would be copied .i.e Done with Copying Texts to the Clipboard Using Java.

My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!