Copy text and image into clipboard using JavaFx example

In this tutorial, let us see how to copy text and image  into clipboard in Java using  JavaFx.    In this example, we will do the following activities
a) Copy Text from Textbox to Clipboard
b) Copy Image from Label to Clipboard
c) Copy Image from File (/image/test.png) to Clipboard
To implement the above activities, we will create a JavaFx scene and necessary fields like Label, TextField, Button.   Also we will create an Utils class which will have the below methods to copy text, image to Clipboard
 void copyToClipboardText(String s) {} – Copies the given string to clipboard.
 void copyToClipboardImage(Label lbl) {}  –  Copies the label’s image to clipboard.
 void copyToClipboardImageFromFile(String path) {} – Copies image from a file to clipboard
Now let us see the complete code.
JavaFx GUI

 

Utils Class

 

Running the above (JavaFxGui2 ) class will create a javaFx scene which will have one textbox, label and three buttons.

When we click on the first button,  the text content  in the textbox is copied to clipboard.  Clicking on Second and third button will copy the image to clipboard. You can paste and check  the image in  MsPaint or any other image editor application.

Leave a Reply