
JavaFX applications are a little different than AWT and Swing applications. StringSelection strSel = new StringSelection(str) Ĭopy Text to Clipboard in JavaFX Applications Import Ĭlipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard() The second argument denotes the owner of the clipboard.The first argument is a transferable instance that, in our case, is the StringSelection class instance.The setContent() method accepts two arguments. To create an instance of the StringSelection class, you must pass the string to be copied to the class constructor.įinally, when the instance is ready, you can call the setContent() method of the clipboard that copies your string to the system clipboard. The StringSelection class implements the functionality to transfer a string in the simple text format.

You can do so by using the StringSelection class. Copy String to Clipboard Using the StringSelection Class in JavaĪfter getting the instance of the clipboard, the next task is to make the string ready to be copied to the clipboard. Once you get the clipboard instance, you are ready to copy the text to the clipboard. The method throws an exception named HeadlessException that represents the headless flag in the application.Īndroid Copy Text to Clipboard Programmatically. public abstract Clipboard getSystemClipboard() The definition of the getSystemClipboard() method is given below. This abstract method returns a singleton instance of the system clipboard. Once you get the default instance of Toolkit, you can use it to invoke the method getSystemClipboard() further. public static Toolkit getDefaultToolkit() The definition of the method is given below.

The class has a static method named getDefaultToolkit() that returns a default instance of the Toolkit class. Java provides a superclass named Toolkit that contains a variety of methods to implement the GUI-based functionalities in Java. Create Instance of Clipboard Using the Toolkit and Clipboard Class in Java Multiple times, the text must be copied to the clipboard to export to other computer applications. Java provides several elements that display the text in a Java application. This article discusses copying the text from a Java application to the clipboard. Copy Text to Clipboard in JavaFX Applications.Copy String to Clipboard Using the StringSelection Class in Java.

Create Instance of Clipboard Using the Toolkit and Clipboard Class in Java.
