- Direct Known Subclasses:
Dragboard
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.
If a security manager is present, the application must have the
FXPermission
"accessClipboard" in order for the
Clipboard returned from Clipboard.getSystemClipboard() to be
accessible from outside the JavaFX application. For compatibility with
previous versions of the JDK the equivalent AWTPermission
"accessClipboard" will also allow the FX clipboard to be accessible from
outside the JavaFX application.
If the application lacks permission or 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.
- Since:
- JavaFX 2.0
-
Method Summary
Modifier and TypeMethodDescriptionfinal void
clear()
Clears the clipboard of any and all content.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.final Set<DataFormat>
Gets the set of DataFormat types on this Clipboard instance which have associated data registered on the clipboard.getFiles()
Gets the List of Files from the clipboard which had previously been registered.final String
getHtml()
Gets the HTML text String from the clipboard which had previously been registered.final Image
getImage()
Gets the Image from the clipboard which had previously been registered.final String
getRtf()
Gets the RTF text String from the clipboard which had previously been registered.final String
Gets the plain text String from the clipboard which had previously been registered.static Clipboard
Gets the current system clipboard, through which data can be stored and retrieved.final String
getUrl()
Gets the URL String from the clipboard which had previously been registered.final boolean
hasContent
(DataFormat dataFormat) Tests whether there is any content on this clipboard of the given DataFormat type.final boolean
hasFiles()
Gets whether an List of Files (DataFormat.FILES) has been registered on this Clipboard.final boolean
hasHtml()
Gets whether an HTML text String (DataFormat.HTML) has been registered on this Clipboard.final boolean
hasImage()
Gets whether an Image (DataFormat.IMAGE) has been registered on this Clipboard.final boolean
hasRtf()
Gets whether an RTF String (DataFormat.RTF) has been registered on this Clipboard.final boolean
Gets whether a plain text String (DataFormat.PLAIN_TEXT) has been registered on this Clipboard.final boolean
hasUrl()
Gets whether a url String (DataFormat.URL) has been registered on this Clipboard.final boolean
setContent
(Map<DataFormat, Object> content) Puts content onto the clipboard.
-
Method Details
-
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.- Returns:
- The single system clipboard, used for cut / copy / paste operations
-
clear
public final void clear()Clears the clipboard of any and all content. Any subsequent call togetContentTypes()
before putting more content on the clipboard will result in an empty set being returned. -
getContentTypes
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 modifed, this returned set is not updated.- Returns:
- A non-null immutable set of content types.
-
setContent
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.- Parameters:
content
- The content to put on the clipboard. If null, the clipboard is simply cleared and no new content added.- Returns:
- True if successful, false if the content fails to be added.
- Throws:
NullPointerException
- if null data reference is passed for any format
-
getContent
Returns the content stored in this clipboard of the given type, or null if there is no content with this type.- Parameters:
dataFormat
- the format type- Returns:
- The content associated with this type, or null if there is none
-
hasContent
Tests whether there is any content on this clipboard of the given DataFormat type.- Parameters:
dataFormat
- the format type- Returns:
- true if there is content on this clipboard for this type
-
hasString
public final boolean hasString()Gets whether a plain text String (DataFormat.PLAIN_TEXT) has been registered on this Clipboard.- Returns:
- true if
hasContent(DataFormat.PLAIN_TEXT)
returns true, false otherwise
-
getString
Gets the plain text String from the clipboard which had previously been registered. This is equivalent to invokinggetContent(DataFormat.PLAIN_TEXT)
. If no such entry exists, null is returned.- Returns:
- The String on the clipboard associated with DataFormat.PLAIN_TEXT, or null if there is not one.
-
hasUrl
public final boolean hasUrl()Gets whether a url String (DataFormat.URL) has been registered on this Clipboard.- Returns:
- true if hasContent(DataFormat.URL) returns true, false otherwise
-
getUrl
Gets the URL String from the clipboard which had previously been registered. This is equivalent to invokinggetContent(DataFormat.URL)
. If no such entry exists, null is returned.- Returns:
- The String on the clipboard associated with DataFormat.URL, or null if there is not one.
-
hasHtml
public final boolean hasHtml()Gets whether an HTML text String (DataFormat.HTML) has been registered on this Clipboard.- Returns:
- true if
hasContent(DataFormat.HTML)
returns true, false otherwise
-
getHtml
Gets the HTML text String from the clipboard which had previously been registered. This is equivalent to invokinggetContent(DataFormat.HTML)
. If no such entry exists, null is returned.- Returns:
- The String on the clipboard associated with DataFormat.HTML, or null if there is not one.
-
hasRtf
public final boolean hasRtf()Gets whether an RTF String (DataFormat.RTF) has been registered on this Clipboard.- Returns:
- true if hasContent(DataFormat.RTF) returns true, false otherwise
-
getRtf
Gets the RTF text String from the clipboard which had previously been registered. This is equivalent to invokinggetContent(DataFormat.RTF)
. If no such entry exists, null is returned.- Returns:
- The String on the clipboard associated with DataFormat.RTF, or null if there is not one.
-
hasImage
public final boolean hasImage()Gets whether an Image (DataFormat.IMAGE) has been registered on this Clipboard.- Returns:
- true if hasContent(DataFormat.IMAGE) returns true, false otherwise
-
getImage
Gets the Image from the clipboard which had previously been registered. This is equivalent to invokinggetContent(DataFormat.IMAGE)
. If no such entry exists, null is returned.- Returns:
- The Image on the clipboard associated with DataFormat.IMAGE, or null if there is not one.
-
hasFiles
public final boolean hasFiles()Gets whether an List of Files (DataFormat.FILES) has been registered on this Clipboard.- Returns:
- true if hasContent(DataFormat.FILES) returns true, false otherwise
-
getFiles
Gets the List of Files from the clipboard which had previously been registered. This is equivalent to invokinggetContent(DataFormat.FILES)
. If no such entry exists, null is returned.- Returns:
- The List of Files on the clipboard associated with DataFormat.FILES, or null if there is not one.
-