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
package javaonline.javafx.ui.test;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javaonline.javafx.util.Utils;

public class JavaFxGui2 extends Application {

	public static int WINDOW_WIDTH = 1200;
	public static int WINDOW_HEIGHT = 500;

	public static void main(String[] args) {


	public void start(final Stage stage) {

		stage.setTitle("JavaFx Example");

		GridPane grid = new GridPane();
		ColumnConstraints column = new ColumnConstraints();
		column = new ColumnConstraints();

		Label lbl1 = new Label("Label2");


		TextField txt1 = new TextField("Text1: The contents of text file located inside resources");

		Button btn2 = new Button();
		btn2.setText("Copy Text from Textbox to Clipboard");

		Button btn3 = new Button();
		btn3.setText("Copy Image from label to Clipboard");

		Button btn4 = new Button();
		btn4.setText("Copy Image from File (/image/test.png) to Clipboard ");

		btn2.setOnAction(new EventHandler<ActionEvent>() {

			public void handle(ActionEvent event) {

				// copy label1 content to clipboard




		btn3.setOnAction(new EventHandler<ActionEvent>() {

			public void handle(ActionEvent event) {

				// copy label image to clipboard




		btn4.setOnAction(new EventHandler<ActionEvent>() {

			public void handle(ActionEvent event) {

				// copy label image to clipboard




		final VBox vbox = new VBox();
		vbox.getChildren().addAll(txt1, lbl1, btn2, btn3, btn4);
		grid.add(vbox, 0, 0);
		Scene scene1 = new Scene(grid, 500, 300);




Utils Class

package javaonline.javafx.util;


import javafx.scene.SnapshotParameters;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;

public class Utils {

//To copy 

	public static void copyToClipboardText(String s) {

		final Clipboard clipboard = Clipboard.getSystemClipboard();
		final ClipboardContent content = new ClipboardContent();



	public static void copyToClipboardImage(Label lbl) {

		WritableImage snapshot = lbl.snapshot(new SnapshotParameters(), null);
		final Clipboard clipboard = Clipboard.getSystemClipboard();
		final ClipboardContent content = new ClipboardContent();



	public static void copyToClipboardImageFromFile(String path) {

		final Clipboard clipboard = Clipboard.getSystemClipboard();
		final ClipboardContent content = new ClipboardContent();



	public static Image getImage(String path) {

		InputStream is = Utils.class.getResourceAsStream(path);
		return new Image(is);
	public static ImageView setIcon(String path) {

		InputStream is = Utils.class.getResourceAsStream(path);
		ImageView iv = new ImageView(new Image(is));

		return iv;



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

male performance enhancement pills cupids closet rhino male enhancement pills to keep a man hard best penis enlargement procedure apexatropin male enhancement last longer pills cvs male enhancement native ads stay erect pills male enhancement clinic san antonio which ed drug works best best male enhancement pills gold drugs that make you want to have sex fury male enhancement pill reviews new erectile dysfunction drugs gnc best male enhancement sizegenix penis enlargement exercise pics stiff nights pill for sale prolong male enhancement and elite 360 natural pills all natural male enhancement reviews compare medicines low sex drive from birth control pills teva birth control pill reviews does gnc sell erectile dysfunction pills best top rated ed pills stiff nights pills wholesale ed supplements max hard over the counter ed drugs that work white pills empty pill capsules gnc penis growth pills in india energy pills walmart what pills good for sex drive pills to increase male libido ginseng sex pills in gas station male enhancement contact blue pill 44 do penis enlargement patches work can i buy male enhancement pills locally extender male enhancement black panther 1000 male enhancement pills biochemical male enhancement

physician weight loss supplements best weight loss pill on the market best pills to take to lose weight pills to lose weight from mexico best diet for perimenopause weight loss easy diet for hypothyroidism to lose weight best weight loss diet ever weight loss supplements for diabetes keto diet instapot recipes top rated weight loss pill keto diet recipes low calorie shark keto diet pills lose weight diet recipes otc pills that make you lose weight build muscle lose weight diet plan effect of caffeine pills on weight loss how to lose weight by running and diet will pills help me lose weight ala supplement weight loss the most effective weight loss diet blood type a diet weight loss weight loss balloon pill illegal drug use weight loss liquid diet for quick weight loss excel weight loss pills scam lemonade diet 10 days weight loss buy keto advamced weight loss pills best quick weight loss diet 2020 how weight loss drugs work horse pills for weight loss weight loss diet on budget cellucor weight loss pills review trishala dutt weight loss diet pills prescribed by doctor to lose weight diet red bull weight loss keto advanced weight loss pills at dischem best diet type for mens weight loss keto diet recipes for two sample diet plan for sero weight loss management fast quick diets lose weight what is alkaline diet for weight loss cheapest diet for weight loss

cbd to thc ratio onda cbd oil select cbd oil reviews cbd oil for arthritis in hands cbd for cte myaderm cbd sport cream cbd stricture gaia cbd oil cbd oil side effects mayo clinic how to calculate cbd dosage cbd mental illness irwin naturals cbd oil maine cbd oil snowboard melbourne cbd spruce cbd review cbd oil spartanburg sc cbd oil smoking cbd and driving bluebird botanicals cbd oil review is vaping cbd bad for you cbd oil and wrinkles cbd gummie best way to take cbd for anxiety best type of cbd oil for anxiety discover cbd colorado meta labs cbd oil review cbd union nj can i use cbd oil like lotion 9 cbd oil not pot cbd gummies cbd oil legal in ma does cannabis oil have thc cbd oil overload tsa cbd oil raid cbd oil murfreesboro can cbd oil trigger migraines is 1000 mg cbd oil good for pain 0 thc cbd gummies cbd oil for stress infographic medical cannabis oil for cancer cannabis oil drops iowa cbd law