Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8095656

[Swing] Drag&Drop with interop on Mac OS X between two processes does not transfer data

    XMLWordPrintable

Details

    Description

      Drag and Drop with Swing in Mac OS x does not work as it should.

      Dragging fro the application attached pure JavaFX to the application attached JavaFX in Swing does not work properly.

      *******************************************************************
      pure JavaFX
      *******************************************************************
      /*
       * To change this license header, choose License Headers in Project Properties.
       * To change this template file, choose Tools | Templates
       * and open the template in the editor.
       */

      package javafxapplication1;

      import javafx.application.Application;
      import javafx.event.EventHandler;
      import javafx.scene.Group;
      import javafx.scene.Scene;
      import javafx.scene.input.*;
      import javafx.scene.paint.Color;
      import javafx.scene.text.Text;
      import javafx.stage.Stage;

      /**
       * Demonstrates a drag-and-drop feature.
       */
      public class JavaFXApplication1 extends Application {

          @Override public void start(Stage stage) {
              stage.setTitle("Hello Drag And Drop");

              Group root = new Group();
              Scene scene = new Scene(root, 400, 200);
              scene.setFill(Color.LIGHTGREEN);

              final Text source = new Text(50, 100, "DRAG ME");
              source.setScaleX(2.0);
              source.setScaleY(2.0);

              final Text target = new Text(250, 100, "DROP HERE");
              target.setScaleX(2.0);
              target.setScaleY(2.0);

              source.setOnDragDetected(new EventHandler <MouseEvent>() {
                  public void handle(MouseEvent event) {
                      /* drag was detected, start drag-and-drop gesture*/
                      System.out.println("onDragDetected");
                      
                      /* allow any transfer mode */
                      Dragboard db = source.startDragAndDrop(TransferMode.ANY);
                      
                      /* put a string on dragboard */
                      ClipboardContent content = new ClipboardContent();
                      content.putString(source.getText());
                      db.setContent(content);
                      
                      event.consume();
                  }
              });

              target.setOnDragOver(new EventHandler <DragEvent>() {
                  public void handle(DragEvent event) {
                      /* data is dragged over the target */
                      System.out.println("onDragOver");
                      
                      /* accept it only if it is not dragged from the same node
                       * and if it has a string data */
                      if (event.getGestureSource() != target &&
                              event.getDragboard().hasString()) {
                          /* allow for both copying and moving, whatever user chooses */
                          event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
                      }
                      
                      event.consume();
                  }
              });

              target.setOnDragEntered(new EventHandler <DragEvent>() {
                  public void handle(DragEvent event) {
                      /* the drag-and-drop gesture entered the target */
                      System.out.println("onDragEntered");
                      /* show to the user that it is an actual gesture target */
                      if (event.getGestureSource() != target &&
                              event.getDragboard().hasString()) {
                          target.setFill(Color.GREEN);
                      }
                      
                      event.consume();
                  }
              });

              target.setOnDragExited(new EventHandler <DragEvent>() {
                  public void handle(DragEvent event) {
                      /* mouse moved away, remove the graphical cues */
                      target.setFill(Color.BLACK);
                      
                      event.consume();
                  }
              });
              
              target.setOnDragDropped(new EventHandler <DragEvent>() {
                  public void handle(DragEvent event) {
                      /* data dropped */
                      System.out.println("onDragDropped");
                      /* if there is a string data on dragboard, read it and use it */
                      Dragboard db = event.getDragboard();
                      boolean success = false;
                      if (db.hasString()) {
                          target.setText(db.getString());
                          success = true;
                      }
                      /* let the source know whether the string was successfully
                       * transferred and used */
                      event.setDropCompleted(success);
                      
                      event.consume();
                  }
              });

              source.setOnDragDone(new EventHandler <DragEvent>() {
                  public void handle(DragEvent event) {
                      /* the drag-and-drop gesture ended */
                      System.out.println("onDragDone");
                      /* if the data was successfully moved, clear it */
                      if (event.getTransferMode() == TransferMode.MOVE) {
                          source.setText("");
                      }
                      
                      event.consume();
                  }
              });

              root.getChildren().add(source);
              root.getChildren().add(target);
              stage.setScene(scene);
              stage.show();
          }

          public static void main(String[] args) {
              Application.launch(args);
          }
      }
      *******************************************************************


      *******************************************************************
      JavaFX in Swing
      *******************************************************************
      /*
       * To change this license header, choose License Headers in Project Properties.
       * To change this template file, choose Tools | Templates
       * and open the template in the editor.
       */

      package javafxapplication1;

      import java.awt.BorderLayout;
      import java.awt.Dimension;
      import javafx.application.Platform;
      import javafx.embed.swing.JFXPanel;
      import javafx.event.EventHandler;
      import javafx.scene.Group;
      import javafx.scene.Scene;
      import javafx.scene.input.ClipboardContent;
      import javafx.scene.input.DragEvent;
      import javafx.scene.input.Dragboard;
      import javafx.scene.input.MouseEvent;
      import javafx.scene.input.TransferMode;
      import javafx.scene.layout.HBox;
      import javafx.scene.layout.StackPane;
      import javafx.scene.text.Text;
      import javafx.scene.paint.Color;
      import javax.swing.JApplet;
      import javax.swing.JFrame;
      import javax.swing.SwingUtilities;
      import javax.swing.UIManager;

      /**
       *
       * @author diego.cirujano-cuesta
       */
      public class NewFXSwingMain extends JApplet {
          
          private static final int JFXPANEL_WIDTH_INT = 300;
          private static final int JFXPANEL_HEIGHT_INT = 250;
          private static JFXPanel fxContainer;

          /**
           * @param args the command line arguments
           */
          public static void main(String[] args) {
              SwingUtilities.invokeLater(new Runnable() {
                  
                  @Override
                  public void run() {
                      try {
                          UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
                      } catch (Exception e) {
                      }
                      
                      JFrame frame = new JFrame("JavaFX 2 in Swing");
                      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                      
                      JApplet applet = new NewFXSwingMain();
                      applet.init();
                      
                      frame.setContentPane(applet.getContentPane());
                      
                      frame.pack();
                      frame.setLocationRelativeTo(null);
                      frame.setVisible(true);
                      
                      applet.start();
                  }
              });
          }
          
          @Override
          public void init() {
              fxContainer = new JFXPanel();
              fxContainer.setPreferredSize(new Dimension(JFXPANEL_WIDTH_INT, JFXPANEL_HEIGHT_INT));
              add(fxContainer, BorderLayout.CENTER);
              // create JavaFX scene
              Platform.runLater(new Runnable() {
                  
                  @Override
                  public void run() {
                      createScene();
                  }
              });
          }
          
          private void createScene() {
              
              Group root = new Group();
                
              final Text source = new Text(50, 100, "DRAG ME");
              source.setScaleX(2.0);
              source.setScaleY(2.0);

              final Text target = new Text(250, 100, "DROP HERE");
              target.setScaleX(2.0);
              target.setScaleY(2.0);

              source.setOnDragDetected(new EventHandler <MouseEvent>() {
                  public void handle(MouseEvent event) {
                      /* drag was detected, start drag-and-drop gesture*/
                      System.out.println("onDragDetected");
                      
                      /* allow any transfer mode */
                      Dragboard db = source.startDragAndDrop(TransferMode.ANY);
                      
                      /* put a string on dragboard */
                      ClipboardContent content = new ClipboardContent();
                      content.putString(source.getText());
                      db.setContent(content);
                      
                      event.consume();
                  }
              });

              target.setOnDragOver(new EventHandler <DragEvent>() {
                  public void handle(DragEvent event) {
                      /* data is dragged over the target */
                      System.out.println("onDragOver");
                      
                      /* accept it only if it is not dragged from the same node
                       * and if it has a string data */
                      if (event.getGestureSource() != target &&
                              event.getDragboard().hasString()) {
                          /* allow for both copying and moving, whatever user chooses */
                          event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
                      }
                      
                      event.consume();
                  }
              });

              target.setOnDragEntered(new EventHandler <DragEvent>() {
                  public void handle(DragEvent event) {
                      /* the drag-and-drop gesture entered the target */
                      System.out.println("onDragEntered");
                      /* show to the user that it is an actual gesture target */
                      if (event.getGestureSource() != target &&
                              event.getDragboard().hasString()) {
                          target.setFill(Color.GREEN);
                      }
                      
                      event.consume();
                  }
              });

              target.setOnDragExited(new EventHandler <DragEvent>() {
                  public void handle(DragEvent event) {
                      /* mouse moved away, remove the graphical cues */
                      target.setFill(Color.BLACK);
                      
                      event.consume();
                  }
              });
              
              target.setOnDragDropped(new EventHandler <DragEvent>() {
                  public void handle(DragEvent event) {
                      /* data dropped */
                      System.out.println("onDragDropped");
                      /* if there is a string data on dragboard, read it and use it */
                      Dragboard db = event.getDragboard();
                      boolean success = false;
                      if (db.hasString()) {
                          target.setText(db.getString());
                          success = true;
                      }
                      /* let the source know whether the string was successfully
                       * transferred and used */
                      event.setDropCompleted(success);
                      
                      event.consume();
                  }
              });

              source.setOnDragDone(new EventHandler <DragEvent>() {
                  public void handle(DragEvent event) {
                      /* the drag-and-drop gesture ended */
                      System.out.println("onDragDone");
                      /* if the data was successfully moved, clear it */
                      if (event.getTransferMode() == TransferMode.MOVE) {
                          source.setText("");
                      }
                      
                      event.consume();
                  }
              });

              root.getChildren().add(source);
              root.getChildren().add(target);
              
              Scene scene = new Scene(root, 400, 200);
              scene.setFill(Color.LIGHTGREEN);
              fxContainer.setScene(scene);
          }
          
      }

      Attachments

        Issue Links

          Activity

            People

              anthony Anthony Petrov (Inactive)
              dcirujanojfx Diego Cirujano (Inactive)
              Votes:
              1 Vote for this issue
              Watchers:
              9 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported: