Getting Image from Clipboard Awt vs FX

不打扰是莪最后的温柔 提交于 2019-12-12 11:06:10

问题


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?

来源:https://stackoverflow.com/questions/48932575/getting-image-from-clipboard-awt-vs-fx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!