Tutorial : How To Copy Text in JavaFX and Swing

At IDRSolutions we have a PDF Viewer that has the ability to highlight and copy text, because we are developing a JavaFX implementation of our PDF Viewer we required a way to copy text in JavaFX that does not use the Swing libraries.

In this tutorial I will show you how to Copy text to the System clipboard in JavaFX and how to do the same in Swing for comparison.

ss


 

Copying Text in JavaFX

Copying text in JavaFX is a simple process, it requires the import of the following classes :

import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;

All we need to do now is to get the String of text that needs copying, this can be from anywhere really but for this example we’ll grab it from a TextField.

String copyText = myTextField.getText();

Now we create a ClipboardContent object which allows us to hold the string of text and a Clipboard object which will allow us to copy the text passed to ClipboardContent to the System Clipboard.

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

And that’s it! Common cases of this would be to put inside some form of Action Listener.

Copying Text in Swing

Copying text in Swing is a similar process to JavaFX, it requires the following classes:

import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;

All we need to do now is to get the String of text that needs copying, this can be from anywhere really but for this example we’ll grab it from a JTextField.

String copyText = myJTextField.getText();

Now we pass the String into a StringSelection object which then gets set as the content of the System Clipboard.

StringSelection ss = new StringSelection(copyText);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);

And that’s it! Common cases of this would be to put inside some form of Listener/Handler.

Thank you for reading, I hope you enjoyed this article and will find it of use. If you have any questions, please post them in the comments section bellow.

You may find some of my other JavaFX articles of interest :

We also have a JavaFX PDF Viewer plugin for NetBeans which you can grab here.

This post is part of our “JavaFX article Index” series. In these articles, we aim to explore the world of Java and Javafx. Have a look through!

If you’re a first-time reader, or simply want to be notified when we post new articles and updates, you can keep up to date by social media (TwitterFacebook and Google+) or the Blog RSS.

Ebook Page Link

The following two tabs change content below.
Nathan is a developer at IDRSolutions, focusing mainly on the development of HTML5 and Form support for the online PDF converter. He was a speaker and exhibitor at JavaOne 2013, co-presenting a session titled 'Lessons Learned from Using GlassFish with NetBeans". Nathan also enjoys writing technical blog-articles.

Related Posts:

Nathan

About Nathan Howard

Nathan is a developer at IDRSolutions, focusing mainly on the development of HTML5 and Form support for the online PDF converter. He was a speaker and exhibitor at JavaOne 2013, co-presenting a session titled 'Lessons Learned from Using GlassFish with NetBeans". Nathan also enjoys writing technical blog-articles.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>