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

cbd lube cbd oil legal tennessee whats the best cbd oil cbd oil missouri 2020 bluebird cbd oil reviews onnit cbd oil what states is cbd oil legal in cbdoil plus cbd merchant account shopify cbd oil brain cancer cbd pie chart cbd to help sleep proof cbd capsules cbd oil canada cbd oil williston nd cbd oil brain cbd oil wholesale colorado cbd products for pain bipolar and cbd oil cbd group collections cbd comfort cream serviced offices perth cbd manitoba cbd oil genuine cbd oil what can you make with cbd oil is cbd oil legal in massachusetts cbd 4 life newry cannabis oil studies directions for taking cbd oil cbd oil with thc uses difference of cbd and hemp oil willoughby cbd oil hemp cbd oil store woodward royal oak cbd appetite control where is vestralife cbd oil grown non hemp cbd oil topical cbd oil derived form cannibis disabled parking sydney cbd will cbd oil effect my child later cbd oil for ovarian cancer

phoenix weight loss pills best green tea pill for weight loss stimulant free weight loss pills aspen clinic weight loss pills diet energy drinks and weight loss most weight loss pills review lemonade diet beyonce weight loss no more diet weight loss baba ramdev diet chart for weight loss do bee pollen pills make you lose weight keto diet advanced weight loss pills side effects vanish weight loss pills can you lose weight on a 2000 calorie diet does a meat and vegetable diet to lose weight i will lose weight this time diet journal protein supplements to lose weight how to lose weight for men in their 60s best diet ketogenic diet menus weight loss weight loss pill white with blue specks how can a ketogenic diet help in weight loss weight loss pills that come with metabolism weight loss over the counter supplements diets pills for quick weight loss best weight loss pills for belly fat for women how do you take keto advanced weight loss pills 500 calories a day diet how much weight loss the best weight loss pills australia can laxative pills make you lose weight sophia pills for weight loss guaranteed weight loss diet plan charlize theron weight loss diet tully how much weight loss on cabbage soup diet how does a vegan diet contribute to weight loss best fiber pills to lose weight why does gluten free diet cause weight loss cardiac fast weight loss diet low carb diets for quick weight loss long term weight loss pills healthy lose weight pills top best diets to lose weight

male genital enhancement cheapest online drugstore alphamax male enhancement mens health ed supplements male enhancement convicted illegal copula male enhancement pills tekmale male enhancement review silicone enlargement to penis heartburn with ed pills roman sex pills best supplements for men ed male enhancement prescription pills does the red pill work penis enlargement cream made in us stigma 9 male enhancement pills droz penis pills male enhancement products affilate venu beauty male enhancement pills review magnum male gold enhancement zyherin male enhancer male enhancement pills g somyhing me otc penis enlargement medicine falsehoods about male enhancer dr oz do male enhancement pills work heat enhancer to attract male dogs male enhancement 2 pills xxl magazine male enhancer omc sex pills penis enlargement united states rx 7 small white pill penis pills 2020 pills for dick giant penis pills black mamba drug ingredients sex enhancement equipment xcream penis enlargement sex with attitude pills sex drive medication triple stacked pills sex with attitude pills