public class Clipboard extends Object
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(java.util.Map<javafx.scene.input.DataFormat, java.lang.Object>)
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. In this case, 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 | Method and Description |
---|---|
void |
clear()
Clears the clipboard of any and all content.
|
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.
|
Set<DataFormat> |
getContentTypes()
Gets the set of DataFormat types on this Clipboard instance which have
associated data registered on the clipboard.
|
List<File> |
getFiles()
Gets the List of Files from the clipboard which had previously
been registered.
|
String |
getHtml()
Gets the HTML text String from the clipboard which had previously
been registered.
|
Image |
getImage()
Gets the Image from the clipboard which had previously
been registered.
|
String |
getRtf()
Gets the RTF text String from the clipboard which had previously
been registered.
|
String |
getString()
Gets the plain text String from the clipboard which had previously
been registered.
|
static Clipboard |
getSystemClipboard()
Gets the current system clipboard, through which data can be stored and
retrieved.
|
String |
getUrl()
Gets the URL String from the clipboard which had previously
been registered.
|
boolean |
hasContent(DataFormat dataFormat)
Tests whether there is any content on this clipboard of the given DataFormat type.
|
boolean |
hasFiles()
Gets whether an List of Files (DataFormat.FILES) has been registered
on this Clipboard.
|
boolean |
hasHtml()
Gets whether an HTML text String (DataFormat.HTML) has been registered
on this Clipboard.
|
boolean |
hasImage()
Gets whether an Image (DataFormat.IMAGE) has been registered
on this Clipboard.
|
boolean |
hasRtf()
Gets whether an RTF String (DataFormat.RTF) has been registered
on this Clipboard.
|
boolean |
hasString()
Gets whether a plain text String (DataFormat.PLAIN_TEXT) has been registered
on this Clipboard.
|
boolean |
hasUrl()
Gets whether a url String (DataFormat.URL) has been registered
on this Clipboard.
|
boolean |
setContent(Map<DataFormat,Object> content)
Puts content onto the clipboard.
|
public static Clipboard getSystemClipboard()
public final void clear()
getContentTypes()
before putting more content on the clipboard
will result in an empty set being returned.public final Set<DataFormat> getContentTypes()
public final boolean setContent(Map<DataFormat,Object> content)
content
- The content to put on the clipboard. If null, the
clipboard is simply cleared and no new content added.NullPointerException
- if null data reference is passed for any
formatpublic final Object getContent(DataFormat dataFormat)
public final boolean hasContent(DataFormat dataFormat)
public final boolean hasString()
hasContent(DataFormat.PLAIN_TEXT)
returns true, false otherwisepublic final String getString()
getContent(DataFormat.PLAIN_TEXT)
. If no such entry exists,
null is returned.public final boolean hasUrl()
public final String getUrl()
getContent(DataFormat.URL)
. If no such entry exists,
null is returned.public final boolean hasHtml()
hasContent(DataFormat.HTML)
returns true, false otherwisepublic final String getHtml()
getContent(DataFormat.HTML)
. If no such entry exists,
null is returned.public final boolean hasRtf()
public final String getRtf()
getContent(DataFormat.RTF)
. If no such entry exists,
null is returned.public final boolean hasImage()
public final Image getImage()
getContent(DataFormat.IMAGE)
. If no such entry exists,
null is returned.public final boolean hasFiles()
public final List<File> getFiles()
getContent(DataFormat.FILES)
. If no such entry exists,
null is returned.Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 2008, 2017, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.