Forum Stats

  • 3,838,727 Users
  • 2,262,395 Discussions


JavaFX Clipboard -- cannot paste image

982084 Member Posts: 6
edited Dec 28, 2012 2:23PM in JavaFX 2.0 and Later
UPDATE at the bottom

I'm trying to build my first full-fledged JavaFX project.

To make a long story short, the app allows you to assign icons/images to records. Specifically, I want to enable the user to paste an image from the system clipboard into the application.

Fortunately, the JavaFX Clipboard API is very developer friendly... but it's simply not working for me.

If I go into MS Paint or Photoshop, and copy an image in the clipboard, the following code won't work:
// Fetch reference to clipboard
Clipboard clipboard = Clipboard.getSystemClipboard();

// Retrieve image, if applicable.
if (clipboard.hasImage())
  Image clipImage = clipboard.getImage();
In the above example, 'clipImage' is always NULL. Even though hasImage() returns true.

Furthermore, I decided to investigate and I ran the following segment:
// Fetch reference to clipboard
Clipboard clipboard = Clipboard.getSystemClipboard();

// Retrieve data formats actually contained in current clipboard
Set<DataFormat> dataFormats = clipboard.getContentTypes();

// For each data format contained...        
for (DataFormat dataFormat : dataFormats)
  System.out.println("Format: " + dataFormat.toString());
  System.out.println("Has data? " 
    + (clipboard.hasContent(dataFormat) ? 
    "yes" : "no"));
  Object myObject = clipboard.getContent(dataFormat);
  System.out.println("Object null? "
    + (myObject == null ? "yes" : "no"));
The following output is produced:
*Format: [application/x-java-rawimage]*
Has data? yes
Raw object null? yes

Apparently, there's just no way for me to fetch a reference to the data that's in the clipboard. I always get a NULL return value.

Anyone can help me understand this?

UPDATE: I would like to add that, if I use my web browser Chrome to find an image, right-click and and select 'Copy image', and then use the JavaFX clipboard feature, it works perfectly. Perhaps I'm missing something...

Edited by: 979081 on Dec 28, 2012 11:22 AM
This discussion has been closed.