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.
- OPEN ANY JAVA IDE, Here we will be using Spring Tool Suite(STS) Java IDE.
- GO TO FILE, After opening Java IDE.
- GO TO NEW i.e. press ALT+SHIFT+N
- 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.
