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) {

		launch(args);
	}

	@Override
	public void start(final Stage stage) {

		stage.setTitle("JavaFx Example");

		GridPane grid = new GridPane();
		grid.setHgap(1);
		grid.setVgap(12);
		grid.setAlignment(Pos.TOP_CENTER);
		ColumnConstraints column = new ColumnConstraints();
		column = new ColumnConstraints();
		column.setPercentWidth(100);
		grid.getColumnConstraints().add(column);

		Label lbl1 = new Label("Label2");
		lbl1.setGraphic(Utils.setIcon("/image/test.png"));

		

		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>() {

			@Override
			public void handle(ActionEvent event) {

				// copy label1 content to clipboard

				Utils.copyToClipboardText(txt1.getText());

			}

		});

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

			@Override
			public void handle(ActionEvent event) {

				// copy label image to clipboard

				Utils.copyToClipboardImage(lbl1);

			}

		});

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

			@Override
			public void handle(ActionEvent event) {

				// copy label image to clipboard

				Utils.copyToClipboardImageFromFile("/image/test.png");

			}

		});

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

	}

}

 

Utils Class

package javaonline.javafx.util;

import java.io.InputStream;

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();

		content.putString(s);
		clipboard.setContent(content);

	}

	public static void copyToClipboardImage(Label lbl) {

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

		content.putImage(snapshot);
		clipboard.setContent(content);

	}

	public static void copyToClipboardImageFromFile(String path) {

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

		content.putImage(Utils.getImage(path));
		clipboard.setContent(content);

	}

	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));

		iv.setFitWidth(100);
		iv.setFitHeight(100);
		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