Wednesday, July 3, 2024
HomeLanguagesJavaHow to Copy Text to the Clipboard in Java?

How to Copy Text to the Clipboard in Java?

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 java.awt.data transfer. Clipboard provides important functionalities of the Graphical User Interface(GUI), namely the COPY(CTRL+C), PASTE(CTRL+V), and CUT(CTRL+X).

Note
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();

Approach

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.

Steps:

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.

Java




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;
 
@SuppressWarnings("serial")
public class copytoClipboard
    extends JFrame implements ActionListener {
   
    JTextField t1;
    JButton b1;
   
    copytoClipboard()
    {
        setLayout(null);
        t1 = new JTextField();
        t1.setBounds(20, 40, 400, 200);
        add(t1);
        b1 = new JButton("COPY CONTENT!");
        b1.setBounds(70, 270, 150, 50);
        b1.addActionListener(this);
        add(b1);
 
        setVisible(true);
        setBounds(200, 90, 500, 500);
    }
   
    public static void main(String args[])
    {
        new copytoClipboard();
    }
   
    @Override
    public void actionPerformed(ActionEvent e)
    {
        String str = t1.getText();
        Clipboard clip = Toolkit.getDefaultToolkit()
                             .getSystemClipboard();
        StringSelection strse1 = new StringSelection(str);
        clip.setContents(strse1, strse1);
        JOptionPane.showMessageDialog(null,
                                      "TEXTS ARE COPIED!");
    }
   
}
 
// This code is contributed by Rahul Chauhan.


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

NOTE: IMPORT NEEDED PACKAGES, CLASSES, AND LIBRARIES.

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.

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments