Lately our Java-FX application can't read Images from the clipboard anymore. For example the user selects a part of an Image in Microsofts-Paint and presses copy. I am not talking about copied image files, those work fine.
I'm pretty sure it worked already in the past, but I still need to verify that. Nevertheless I created a small example and compared the AWT with the FX clipboard to reproduce the behaviour:
public class ClipBoardFxApp extends Application
{
@Override
public void start( final Stage primaryStage )
{
final BorderPane root = new BorderPane();
final ImageView view = new ImageView();
final Button awtButton = new Button( "AWT" );
awtButton.setOnAction( event -> loadImageFromAwtClipboard( view ) );
final Button javaFXButton = new Button( "JavaFX" );
javaFXButton.setOnAction( event -> loadImageFromJavaFXClipboard( view ) );
root.setCenter( view );
final BorderPane buttonPane = new BorderPane();
buttonPane.setLeft( awtButton );
buttonPane.setRight( javaFXButton );
root.setBottom( buttonPane );
final Scene scene = new Scene( root, 400, 400 );
primaryStage.setScene( scene );
primaryStage.show();
}
private void loadImageFromJavaFXClipboard( final ImageView view )
{
System.out.println( "FX-Clipboard: Try to add Image from Clipboard..." );
final Clipboard clipboard = Clipboard.getSystemClipboard();
if ( clipboard.hasImage() )
{
final Image image = clipboard.getImage();
view.setImage( image );
}
else
{
new Alert( AlertType.INFORMATION, "No Image detected im Clipboard!" ).show();
}
}
private void loadImageFromAwtClipboard( final ImageView view )
{
System.out.println( "AWT-Clipboard: Try to add Image from Clipboard..." );
try
{
final Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents( null );
if ( t != null && t.isDataFlavorSupported( DataFlavor.imageFlavor ) )
{
final java.awt.image.BufferedImage img = (java.awt.image.BufferedImage) t.getTransferData( DataFlavor.imageFlavor );
final Image image = SwingFXUtils.toFXImage( img, null );
view.setImage( image );
}
else
{
new Alert( AlertType.INFORMATION, "No Image detected im Clipboard!" ).show();
}
}
catch ( final UnsupportedFlavorException | IOException e )
{
e.printStackTrace();
}
}
public static void main( final String[] args )
{
launch( args );
}
}
The AWT-Clipboard works as expected, where as the Java-FX Clipboard only shows a white image with the correct size of the image, but without content. I'd prefer to stay the FX-way, so is there any solution to that or any explanation, what I might do wrong here?
Product Version: NetBeans IDE 8.2 (Build 201609300101) Updates: NetBeans IDE is updated to version NetBeans 8.2 Patch 2 Java: 1.8.0_161; Java HotSpot(TM) 64-Bit Server VM 25.161-b12 Runtime: Java(TM) SE Runtime Environment 1.8.0_161-b12 System: Windows 10 version 10.0 running on amd64; Cp1252; en_US (nb)
– Cato