Advertisement
Guest User

Fix for javafx clipboard issue.

a guest
Feb 23rd, 2020
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.26 KB | None | 0 0
  1. package sample;
  2.  
  3. import com.sun.glass.ui.ClipboardAssistance;
  4. import javafx.application.Application;
  5. import javafx.embed.swing.SwingFXUtils;
  6. import javafx.geometry.Insets;
  7. import javafx.geometry.Pos;
  8. import javafx.scene.Group;
  9. import javafx.scene.Scene;
  10. import javafx.scene.control.Alert;
  11. import javafx.scene.control.Button;
  12. import javafx.scene.control.ColorPicker;
  13. import javafx.scene.control.Label;
  14. import javafx.scene.effect.BlendMode;
  15. import javafx.scene.image.Image;
  16. import javafx.scene.image.ImageView;
  17. import javafx.scene.input.Clipboard;
  18. import javafx.scene.layout.*;
  19. import javafx.scene.paint.Color;
  20. import javafx.scene.shape.Rectangle;
  21. import javafx.stage.Stage;
  22.  
  23. import java.awt.*;
  24. import java.awt.datatransfer.DataFlavor;
  25. import java.awt.datatransfer.Transferable;
  26. import java.awt.datatransfer.UnsupportedFlavorException;
  27. import java.io.IOException;
  28.  
  29. /**
  30.  * A program to test out the differences between the AWT Java Clipboard and the JavaFX Clipboard.
  31.  * <p>
  32.  * Based on the code by crusam: https://stackoverflow.com/q/48932575/5432315
  33.  */
  34. public class ClipBoardFxAwtComparison extends Application {
  35.  
  36.     @Override
  37.     public void start(final Stage primaryStage) {
  38.         final BorderPane root = new BorderPane();
  39.         final Group group = new Group();
  40.  
  41.         final Button awtButton = new Button("AWT");
  42.         awtButton.setOnAction(event -> loadImageFromAWTClipboard(group));
  43.  
  44.         final Button javaFXButton = new Button("JavaFX");
  45.         javaFXButton.setOnAction(event -> loadImageFromJavaFXClipboard(group));
  46.  
  47.         root.setCenter(group);
  48.  
  49.         final BorderPane buttonPane = new BorderPane();
  50.         buttonPane.setLeft(awtButton);
  51.         buttonPane.setCenter(new Label("Copy and image then click a button to paste it."));
  52.         buttonPane.setRight(javaFXButton);
  53.         root.setBottom(buttonPane);
  54.  
  55.         final ColorPicker colorPicker = new ColorPicker();
  56.         colorPicker.setOnAction(event -> {
  57.             root.setBackground(
  58.                     new Background(
  59.                             new BackgroundFill(
  60.                                     colorPicker.getValue(),
  61.                                     CornerRadii.EMPTY,
  62.                                     Insets.EMPTY)
  63.                     )
  64.             );
  65.         });
  66.  
  67.         BorderPane.setAlignment(colorPicker, Pos.CENTER);
  68.         root.setTop(colorPicker);
  69.  
  70.         final Scene scene = new Scene(root, 400, 400);
  71.         primaryStage.setScene(scene);
  72.         primaryStage.show();
  73.  
  74.         listenToClipboard();
  75.     }
  76.  
  77.     /**
  78.      * This little bit of magic comes courtesy of Александр Савостьянов: https://stackoverflow.com/a/47550034/5432315
  79.      */
  80.     private void listenToClipboard() {
  81.         Clipboard clipboard = Clipboard.getSystemClipboard();
  82.         new ClipboardAssistance(com.sun.glass.ui.Clipboard.SYSTEM) {
  83.             @Override
  84.             public void contentChanged() {
  85.                 System.out.println("System clipboard content changed.");
  86.                 System.out.println("Clipboard content types: ");
  87.                 clipboard.getContentTypes().forEach(System.out::println);
  88.                 System.out.println();
  89.             }
  90.         };
  91.     }
  92.  
  93.     private void loadImageFromAWTClipboard(final Group group) {
  94.         System.out.println("Adding an image from the JavaFX Clipboard...");
  95.         final Clipboard clipboard = Clipboard.getSystemClipboard();
  96.  
  97.         try {
  98.             final Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
  99.  
  100.             if (t != null && t.isDataFlavorSupported(DataFlavor.imageFlavor)) {
  101.                 final java.awt.image.BufferedImage img = (java.awt.image.BufferedImage) t.getTransferData(DataFlavor.imageFlavor);
  102.  
  103.                 final Image image = SwingFXUtils.toFXImage(img, null);
  104.  
  105.                 setupImageFixingGroup(group, image);
  106.             } else {
  107.                 new Alert(Alert.AlertType.INFORMATION, "No image detected on the Clipboard!").show();
  108.                 group.getChildren().clear();
  109.             }
  110.         } catch (final UnsupportedFlavorException | IOException e) {
  111.             e.printStackTrace();
  112.         }
  113.     }
  114.  
  115.     private void loadImageFromJavaFXClipboard(final Group group) {
  116.         System.out.println("Adding an image from the JavaFX Clipboard...");
  117.         final Clipboard clipboard = Clipboard.getSystemClipboard();
  118.  
  119.         if (clipboard.hasImage()) {
  120.             final Image image = clipboard.getImage();
  121.  
  122.             setupImageFixingGroup(group, image);
  123.         } else {
  124.             new Alert(Alert.AlertType.INFORMATION, "No image detected on the Clipboard!").show();
  125.             group.getChildren().clear();
  126.         }
  127.     }
  128.  
  129.     private void setupImageFixingGroup(Group group, Image image) {
  130.         final ImageView view = new ImageView(image);
  131.         view.setBlendMode(BlendMode.LIGHTEN);
  132.  
  133.         final Rectangle blend = new Rectangle(image.getWidth(), image.getHeight(), Color.BLACK);
  134.         blend.widthProperty().bind(image.widthProperty());
  135.         blend.heightProperty().bind(image.heightProperty());
  136.  
  137.         group.getChildren().clear();
  138.         group.getChildren().addAll(blend, view);
  139.     }
  140.  
  141.     public static void main(final String[] args) {
  142.         launch(args);
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement