Top Description Fields Constructors Methods
javafx.scene.input

public Class Clipboard

extends Object
Class Inheritance
Known Direct Subclasses
javafx.scene.input.Dragboard
Imports
com.sun.javafx.scene.input.ClipboardHelper, java.io.File, java.util.List, .Map, .Set, javafx.scene.image.Image, javafx.util.Pair, com.sun.javafx.tk.TKClipboard, .Toolkit

Represents an operating system clipboard, on which data may be placed during, for example, cut, copy, and paste operations.

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.

Since
JavaFX 2.0

Field Summary

Modifier and TypeField and Description
private boolean
contentPut

Whether user has put something on this clipboard.

private static Clipboard
pack-priv TKClipboard
private static Clipboard

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public final void
clear()

Clears the clipboard of any and all content.

pack-priv boolean
public final Object

Returns:

The content associated with this type, or null if there is none
getContent
(DataFormat
the 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
getContentImpl(DataFormat dataFormat)

Getting content overridable by internal subclasses.

public final Set<DataFormat>

Returns:

A non-null immutable set of content types.
getContentTypes
()

Gets the set of DataFormat types on this Clipboard instance which have associated data registered on the clipboard.

public final List<File>

Returns:

The List of Files on the clipboard associated with DataFormat.FILES, or null if there is not one.
getFiles
()

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.
getHtml
()

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.
getImage
()

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.
getRtf
()

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.
getString
()

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 operations
getSystemClipboard
()

Gets 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.
getUrl
()

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 type
hasContent
(DataFormat
the 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 otherwise
hasFiles
()

Gets whether an List of Files (DataFormat.FILES) has been registered on this Clipboard.

public final boolean

Returns:

true if hasContent(DataFormat.HTML) returns true, false otherwise
hasHtml
()

Gets 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 otherwise
hasImage
()

Gets whether an Image (DataFormat.IMAGE) has been registered on this Clipboard.

public final boolean

Returns:

true if hasContent(DataFormat.RTF) returns true, false otherwise
hasRtf
()

Gets whether an RTF String (DataFormat.RTF) has been registered on this Clipboard.

public final boolean

Returns:

true if hasContent(DataFormat.PLAIN_TEXT) returns true, false otherwise
hasString
()

Gets 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 otherwise
hasUrl
()

Gets 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.
setContent
(Map<DataFormat, Object>
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.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

contentPutback to summary
private boolean contentPut

Whether user has put something on this clipboard. Needed for DnD.

localClipboardback to summary
private static Clipboard localClipboard
peerback to summary
pack-priv TKClipboard peer
systemClipboardback to summary
private static Clipboard systemClipboard

Constructor Detail

Clipboardback to summary
pack-priv Clipboard(TKClipboard peer)

Method Detail

clearback to summary
public final void clear()

Clears the clipboard of any and all content. Any subsequent call to getContentTypes before putting more content on the clipboard will result in an empty set being returned.

contentPutback to summary
pack-priv boolean contentPut()
getContentback 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.

Parameters
dataFormat:DataFormat

the format type

Returns:Object

The content associated with this type, or null if there is none

getContentImplback to summary
pack-priv Object getContentImpl(DataFormat dataFormat)

Getting content overridable by internal subclasses.

getContentTypesback to summary
public final Set<DataFormat> 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 modified, this returned set is not updated.

Returns:Set<DataFormat>

A non-null immutable set of content types.

getFilesback to summary
public final List<File> getFiles()

Gets the List of Files from the clipboard which had previously been registered. This is equivalent to invoking getContent(DataFormat.FILES). If no such entry exists, null is returned.

Returns:List<File>

The List of Files on the clipboard associated with DataFormat.FILES, or null if there is not one.

getHtmlback to summary
public final String getHtml()

Gets the HTML text String from the clipboard which had previously been registered. This is equivalent to invoking getContent(DataFormat.HTML). If no such entry exists, null is returned.

Returns:String

The String on the clipboard associated with DataFormat.HTML, or null if there is not one.

getImageback to summary
public final Image getImage()

Gets the Image from the clipboard which had previously been registered. This is equivalent to invoking getContent(DataFormat.IMAGE). If no such entry exists, null is returned.

Returns:Image

The Image on the clipboard associated with DataFormat.IMAGE, or null if there is not one.

getLocalClipboardImplback to summary
private static synchronized Clipboard getLocalClipboardImpl()
getRtfback to summary
public final String getRtf()

Gets the RTF text String from the clipboard which had previously been registered. This is equivalent to invoking getContent(DataFormat.RTF). If no such entry exists, null is returned.

Returns:String

The String on the clipboard associated with DataFormat.RTF, or null if there is not one.

getStringback to summary
public final String getString()

Gets the plain text String from the clipboard which had previously been registered. This is equivalent to invoking getContent(DataFormat.PLAIN_TEXT). If no such entry exists, null is returned.

Returns:String

The String on the clipboard associated with DataFormat.PLAIN_TEXT, or null if there is not one.

getSystemClipboardback 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.

Returns:Clipboard

The single system clipboard, used for cut / copy / paste operations

getSystemClipboardImplback to summary
private static synchronized Clipboard getSystemClipboardImpl()
getUrlback to summary
public final String getUrl()

Gets the URL String from the clipboard which had previously been registered. This is equivalent to invoking getContent(DataFormat.URL). If no such entry exists, null is returned.

Returns:String

The String on the clipboard associated with DataFormat.URL, or null if there is not one.

hasContentback to summary
public final boolean hasContent(DataFormat dataFormat)

Tests whether there is any content on this clipboard of the given DataFormat type.

Parameters
dataFormat:DataFormat

the format type

Returns:boolean

true if there is content on this clipboard for this type

hasFilesback to summary
public final boolean hasFiles()

Gets whether an List of Files (DataFormat.FILES) has been registered on this Clipboard.

Returns:boolean

true if hasContent(DataFormat.FILES) returns true, false otherwise

hasHtmlback to summary
public final boolean hasHtml()

Gets whether an HTML text String (DataFormat.HTML) has been registered on this Clipboard.

Returns:boolean

true if hasContent(DataFormat.HTML) returns true, false otherwise

hasImageback to summary
public final boolean hasImage()

Gets whether an Image (DataFormat.IMAGE) has been registered on this Clipboard.

Returns:boolean

true if hasContent(DataFormat.IMAGE) returns true, false otherwise

hasRtfback to summary
public final boolean hasRtf()

Gets whether an RTF String (DataFormat.RTF) has been registered on this Clipboard.

Returns:boolean

true if hasContent(DataFormat.RTF) returns true, false otherwise

hasStringback to summary
public final boolean hasString()

Gets whether a plain text String (DataFormat.PLAIN_TEXT) has been registered on this Clipboard.

Returns:boolean

true if hasContent(DataFormat.PLAIN_TEXT) returns true, false otherwise

hasUrlback to summary
public final boolean hasUrl()

Gets whether a url String (DataFormat.URL) has been registered on this Clipboard.

Returns:boolean

true if hasContent(DataFormat.URL) returns true, false otherwise

setContentback 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.

Parameters
content:Map<DataFormat, Object>

The content to put on the clipboard. If null, the clipboard is simply cleared and no new content added.

Returns:boolean

True if successful, false if the content fails to be added.

Exceptions
NullPointerException:
if null data reference is passed for any format