Getting Image from Clipboard Awt vs FX
Asked Answered
O

0

5

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:

enter image description here

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?

Orthogonal answered 22/2, 2018 at 16:32 Comment(5)
Is it possible your image is just displayed as if severely washed out, as described in #34643102?Pledget
Your code worked for me.Cato
@Pledget I tried having a closer look at the image, but I'm pretty sure its just a white block without content. I testet different programs now, and with programs like paint.net for example, it works fine. But with some GIS-Systems or the normal PAINT from microsoft it has the effect discribed above.Orthogonal
@Sedrick thanks for your feedback. Thats interesting to hear. Can you maybe tell me, what programm you copied an image from and what Version of Java-Runtime you used?Orthogonal
I used Google Images. I search for house and right clicked and copied image on the first image. 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

© 2022 - 2024 — McMap. All rights reserved.