To access the general system clipboard, use the following code:
Clipboard clipboard = Clipboard.getSystemClipboard();
There is only ever one instance of the system clipboard in the application, so it is perfectly acceptable to stash a reference to it somewhere handy if you so choose.
The Clipboard operates on the concept of having a single conceptual item on the clipboard at any one time -- though it may be placed on the clipboard in different formats. For example, the user might select text in an HTML editor and press the ctrl+c or cmd+c to copy it. In this case, the same text might be available on the clipboard both as HTML and as plain text. There are two copies of the data on the clipboard, but they both represent the same data.
Content is specified on the Clipboard by using the setContent
method. First, construct a ClipboardContent object, then invoke setContent. Every time
setContent is called, any previous data on the clipboard is cleared and replaced with
this new content.
final Clipboard clipboard = Clipboard.getSystemClipboard();
final ClipboardContent content = new ClipboardContent();
content.putString("Some text");
content.putHtml("<b>Some</b> text");
clipboard.setContent(content);
The ClipboardContent class is simply a map with convenience methods for dealing
with common data types added to a clipboard.
Because multiple representations of the same data may exist on the clipboard, and because different applications have different capabilities for handling different content types, it is important to place as many data representations on the clipboard as is practical to facilitate external applications. Note that sometimes the operating system might be helpful in some cases and add multiple types for you. For example, the Mac might set the plain text string for you when you specify the RTF type. How and under what circumstances this occurs is outside the realm of this specification, consult your OS documentation.
When reading data off the clipboard, it is important to look for the richest supported type first. For example, if I have a text document which supports embedding of images and media formats, when pasting content from the clipboard I should first check to see if the content can be represented as media or as an image. If not, then I might check for RTF or HTML or whatever rich text format is supported by my document type. If not, then I might just take a String.
Or for example, if I have a plain text document, then I would simple get a String representation and use that, if available. I can check to see if the clipboard "hasHtml" or "hasString".
if (clipboard.hasString()) { ... }
In addition to the common or built in types, you may put any arbitrary data onto the clipboard (assuming it is serializable).
Content types are defined by the DataFormat objects. The DataFormat class defines an immutable object, and there are a number of static final fields for common DataFormat types. Of course application specific DataFormat types can also be declared and used. The following two methods are equivalent (and the second call will override the first!)
ClipboardContent content = new ClipboardContent();
content.putString("some text");
content.put(DataFormat.PLAIN_TEXT, "other text");
On embedded platforms that do not have their own windowing system, the Clipboard returned from Clipboard.getSystemClipboard() might not be accessible from outside the JavaFX application.
If the platform doesn't support a shared clipboard, the clipboard returned by Clipboard.getSystemClipboard() can be used for exchange of data between different parts of one JavaFX application but cannot be used to exchange data between multiple applications.
| Modifier and Type | Field and Description |
|---|---|
| private boolean | contentPut
Whether user has put something on this clipboard. |
| private static Clipboard | |
| pack-priv TKClipboard | |
| private static Clipboard |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public final void | |
| pack-priv boolean | |
| public final Object | Returns: The content associated with this type, or null if there is nonethe format type dataFormat)Returns the content stored in this clipboard of the given type, or null if there is no content with this type. |
| pack-priv Object | |
| public final Set | Returns: A non-null immutable set of content types.Gets the set of DataFormat types on this Clipboard instance which have associated data registered on the clipboard. |
| public final List | Returns: The List of Files on the clipboard associated with DataFormat.FILES, or null if there is not one.Gets the List of Files from the clipboard which had previously been registered. |
| public final String | Returns: The String on the clipboard associated with DataFormat.HTML, or null if there is not one.Gets the HTML text String from the clipboard which had previously been registered. |
| public final Image | Returns: The Image on the clipboard associated with DataFormat.IMAGE, or null if there is not one.Gets the Image from the clipboard which had previously been registered. |
| private static synchronized Clipboard | |
| public final String | Returns: The String on the clipboard associated with DataFormat.RTF, or null if there is not one.Gets the RTF text String from the clipboard which had previously been registered. |
| public final String | Returns: The String on the clipboard associated with DataFormat.PLAIN_TEXT, or null if there is not one.Gets the plain text String from the clipboard which had previously been registered. |
| public static Clipboard | Returns: The single system clipboard, used for cut / copy / paste operationsGets the current system clipboard, through which data can be stored and retrieved. |
| private static synchronized Clipboard | |
| public final String | Returns: The String on the clipboard associated with DataFormat.URL, or null if there is not one.Gets the URL String from the clipboard which had previously been registered. |
| public final boolean | Returns: true if there is content on this clipboard for this typethe format type dataFormat)Tests whether there is any content on this clipboard of the given DataFormat type. |
| public final boolean | Returns: true if hasContent(DataFormat.FILES) returns true, false otherwiseGets whether an List of Files (DataFormat.FILES) has been registered on this Clipboard. |
| public final boolean | Returns: true ifhasContent(DataFormat.HTML) returns true, false otherwiseGets whether an HTML text String (DataFormat.HTML) has been registered on this Clipboard. |
| public final boolean | Returns: true if hasContent(DataFormat.IMAGE) returns true, false otherwiseGets whether an Image (DataFormat.IMAGE) has been registered on this Clipboard. |
| public final boolean | Returns: true if hasContent(DataFormat.RTF) returns true, false otherwiseGets whether an RTF String (DataFormat.RTF) has been registered on this Clipboard. |
| public final boolean | Returns: true ifhasContent(DataFormat.PLAIN_TEXT) returns true, false otherwiseGets whether a plain text String (DataFormat.PLAIN_TEXT) has been registered on this Clipboard. |
| public final boolean | Returns: true if hasContent(DataFormat.URL) returns true, false otherwiseGets whether a url String (DataFormat.URL) has been registered on this Clipboard. |
| public final boolean | Returns: True if successful, false if the content fails to be added.The content to put on the clipboard. If null, the
clipboard is simply cleared and no new content added. content)Puts content onto the clipboard. |
| contentPut | back to summary |
|---|---|
| private boolean contentPut Whether user has put something on this clipboard. Needed for DnD. | |
| localClipboard | back to summary |
|---|---|
| private static Clipboard localClipboard | |
| peer | back to summary |
|---|---|
| pack-priv TKClipboard peer | |
| systemClipboard | back to summary |
|---|---|
| private static Clipboard systemClipboard | |
| Clipboard | back to summary |
|---|---|
| pack-priv Clipboard(TKClipboard peer) | |
| clear | back to summary |
|---|---|
| public final void clear() Clears the clipboard of any and all content. Any subsequent call to
| |
| contentPut | back to summary |
|---|---|
| pack-priv boolean contentPut() | |
| getContent | back to summary |
|---|---|
| public final Object getContent(DataFormat dataFormat) Returns the content stored in this clipboard of the given type, or null if there is no content with this type.
| |
| getContentImpl | back to summary |
|---|---|
| pack-priv Object getContentImpl(DataFormat dataFormat) Getting content overridable by internal subclasses. | |
| getContentTypes | back to summary |
|---|---|
| public final Set Gets the set of DataFormat types on this Clipboard instance which have associated data registered on the clipboard. This set will always be non-null and immutable. If the Clipboard is subsequently modified, this returned set is not updated.
| |
| getFiles | back to summary |
|---|---|
| public final List Gets the List of Files from the clipboard which had previously
been registered. This is equivalent to invoking
| |
| getHtml | back to summary |
|---|---|
| public final String getHtml() Gets the HTML text String from the clipboard which had previously
been registered. This is equivalent to invoking
| |
| getImage | back to summary |
|---|---|
| public final Image getImage() Gets the Image from the clipboard which had previously
been registered. This is equivalent to invoking
| |
| getLocalClipboardImpl | back to summary |
|---|---|
| private static synchronized Clipboard getLocalClipboardImpl() | |
| getRtf | back to summary |
|---|---|
| public final String getRtf() Gets the RTF text String from the clipboard which had previously
been registered. This is equivalent to invoking
| |
| getString | back to summary |
|---|---|
| public final String getString() Gets the plain text String from the clipboard which had previously
been registered. This is equivalent to invoking
| |
| getSystemClipboard | back to summary |
|---|---|
| public static Clipboard getSystemClipboard() Gets the current system clipboard, through which data can be stored and retrieved. There is ever only one system clipboard for a JavaFX application.
| |
| getSystemClipboardImpl | back to summary |
|---|---|
| private static synchronized Clipboard getSystemClipboardImpl() | |
| getUrl | back to summary |
|---|---|
| public final String getUrl() Gets the URL String from the clipboard which had previously
been registered. This is equivalent to invoking
| |
| hasContent | back to summary |
|---|---|
| public final boolean hasContent(DataFormat dataFormat) Tests whether there is any content on this clipboard of the given DataFormat type.
| |
| hasFiles | back to summary |
|---|---|
| public final boolean hasFiles() Gets whether an List of Files (DataFormat.FILES) has been registered on this Clipboard.
| |
| hasHtml | back to summary |
|---|---|
| public final boolean hasHtml() Gets whether an HTML text String (DataFormat.HTML) has been registered on this Clipboard.
| |
| hasImage | back to summary |
|---|---|
| public final boolean hasImage() Gets whether an Image (DataFormat.IMAGE) has been registered on this Clipboard.
| |
| hasRtf | back to summary |
|---|---|
| public final boolean hasRtf() Gets whether an RTF String (DataFormat.RTF) has been registered on this Clipboard.
| |
| hasString | back to summary |
|---|---|
| public final boolean hasString() Gets whether a plain text String (DataFormat.PLAIN_TEXT) has been registered on this Clipboard.
| |
| hasUrl | back to summary |
|---|---|
| public final boolean hasUrl() Gets whether a url String (DataFormat.URL) has been registered on this Clipboard.
| |
| setContent | back to summary |
|---|---|
| public final boolean setContent(Map<DataFormat, Object> content) Puts content onto the clipboard. This call will always result in clearing all previous content from the clipboard, and replacing it with whatever content is specified in the supplied ClipboardContent map.
| |