Java Copy Text to Clipboard
-
Create Instance of Clipboard Using the
Toolkit
andClipboard
Class in Java -
Copy String to Clipboard Using the
StringSelection
Class in Java - Copy Text to Clipboard in JavaFX Applications

This article discusses copying the text from a Java application to the clipboard.
Java provides several elements that display the text in a Java application. Multiple times, the text must be copied to the clipboard to export to other computer applications.
Create Instance of Clipboard Using the Toolkit
and Clipboard
Class in Java
Java provides a superclass named Toolkit
that contains a variety of methods to implement the GUI-based functionalities in Java.
The class has a static method named getDefaultToolkit()
that returns a default instance of the Toolkit
class. The definition of the method is given below.
public static Toolkit getDefaultToolkit()
Once you get the default instance of Toolkit
, you can use it to invoke the method getSystemClipboard()
further. This abstract method returns a singleton instance of the system clipboard.
The definition of the getSystemClipboard()
method is given below.
public abstract Clipboard getSystemClipboard()
throws HeadlessException
The method throws an exception named HeadlessException
that represents the headless flag in the application.
Once you get the clipboard instance, you are ready to copy the text to the clipboard.
Copy String to Clipboard Using the StringSelection
Class in Java
After getting the instance of the clipboard, the next task is to make the string ready to be copied to the clipboard. You can do so by using the StringSelection
class.
The StringSelection
class implements the functionality to transfer a string in the simple text format. This class is a subclass of the java.awt.datatransfer
class.
To create an instance of the StringSelection
class, you must pass the string to be copied to the class constructor.
Finally, when the instance is ready, you can call the setContent()
method of the clipboard that copies your string to the system clipboard. The setContent()
method accepts two arguments.
- The first argument is a transferable instance that, in our case, is the
StringSelection
class instance. - The second argument denotes the owner of the clipboard.
Let us see the code.
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
public class CopyToClipBoard {
public static void main(String [] args)
{
String str = "To be copied";
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
StringSelection strSel = new StringSelection(str);
cb.setContents(strSel, null);
}
}
Copy Text to Clipboard in JavaFX Applications
JavaFX applications are a little different than AWT and Swing applications. You can use a similar functionality with a bit of change to display the HTML content while working with JavaFX applications.
Instead of putting the content directly to the clipboard, you can set the content to the ClipboardContent
instance. Once the content is pushed to the instance, you can directly add that instance to the clipboard.
Similarly, while reading the content, you can differentiate between the normal text and the HTML content using the flag DataFormat.HTML
.
Let us see the example in code.
public class CopyToClipBoard {
Clipboard cb = Clipboard.getSystemClipboard();
ClipboardContent ct = new ClipboardContent();
ct.putString("Text to be copied");
ct.putHtml("<b>HTML Text</b>");
cb.setContent(ct);
if(cb.hasContent(DataFormat.HTML))
{
System.out.println(cb.getHtml());
}
if(cb.hasString())
{
System.out.println(cb.getString());
}
}
That is all about copying the content from a Java application to the system clipboard.
Related Article - Java JavaFX
- InvocationTargetException in JavaFX
- Play a Video Using JavaFX
- Solution to the JavaFX FXML Load Exception
- Create JavaFX Message Box
- JavaFX Media Player
- Create Square in JavaFX