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

[Swing] Drag&Drop with interop on Mac OS X between two JFXPanes throws exceptions

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P4
    • Resolution: Cannot Reproduce
    • Affects Version/s: 7u45, 8
    • Fix Version/s: None
    • Component/s: javafx
    • Environment:

      Mac OS X 10.8.3. MacBook Pro. Java 1.7.0_45(JavaFX 2.2.45-b18)

      Description

      NOTE.- A swing JFrame contains two JFXPane. Each JFXPane contains two nodes "DRAG ME" "DROP ME"

      Running the following application and trying to drag from one of the JFXPane and drop to the other JFXPane throws several exceptions.

      package javafxapplication;

      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.paint.Color;
      import javafx.scene.text.Text;

      public class SceneCreator {
          
          public static Scene createSceneObject(){

              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>() {
                  @Override
                  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 an object on dragboard */
                      ClipboardContent content = new ClipboardContent();
                      content.put(Object.DATA_FORMAT, new Object("Hola mundo"));
                      db.setContent(content);
                      
                      event.consume();
                  }
              });

              target.setOnDragOver(new EventHandler <DragEvent>() {
                  @Override
                  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().getContent(Object.DATA_FORMAT)!=null) {
                          /* allow for both copying and moving, whatever user chooses */
                          event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
                      }
                      
                      event.consume();
                  }
              });

              target.setOnDragEntered(new EventHandler <DragEvent>() {
                  @Override
                  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().hasContent(Object.DATA_FORMAT)) {
                          target.setFill(Color.BLUE);
                      }
                      
                      event.consume();
                  }
              });

              target.setOnDragExited(new EventHandler <DragEvent>() {
                  @Override
                  public void handle(DragEvent event) {
                      /* mouse moved away, remove the graphical cues */
                      target.setFill(Color.BLACK);
                      
                      event.consume();
                  }
              });
              
              target.setOnDragDropped(new EventHandler <DragEvent>() {
                  @Override
                  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.hasContent(Object.DATA_FORMAT)) {
                          target.setText(db.getContent(Object.DATA_FORMAT).toString());
                          System.out.println(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>() {
                  @Override
                  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.LIGHTBLUE);
              return scene;
          }
          
      }
      //***********************************************************************

      package javafxapplication;

      import java.awt.BorderLayout;
      import java.awt.Dimension;
      import javafx.application.Platform;
      import javafx.embed.swing.JFXPanel;

      public class NewJFrame extends javax.swing.JFrame {

          /**
           * Creates new form NewJFrame
           */
          public NewJFrame() {
                      
              addJFXPanelDnDObject();
              addJFXPanelDnDObject();
              
              initComponents();
          }
              
          private void addJFXPanelDnDObject(){
              final JFXPanel fxContainer = new JFXPanel();
                      fxContainer.setPreferredSize(new Dimension(400, 250));
          
              // create JavaFX scene
              Platform.runLater(new Runnable() {
                  
                  @Override
                  public void run() {
                      fxContainer.setScene(SceneCreator.createSceneObject());
                  }
              });
                  getContentPane().add(fxContainer);
          }

          /**
           * This method is called from within the constructor to initialize the form.
           * WARNING: Do NOT modify this code. The content of this method is always
           * regenerated by the Form Editor.
           */
          @SuppressWarnings("unchecked")
          // <editor-fold defaultstate="collapsed" desc="Generated Code">
          private void initComponents() {

              setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
              getContentPane().setLayout(new java.awt.FlowLayout());

              pack();
          }// </editor-fold>

          /**
           * @param args the command line arguments
           */
          public static void main(String args[]) {
              /* Set the Nimbus look and feel */
              //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
              /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
               * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
               */
              try {
                  for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                      if ("Nimbus".equals(info.getName())) {
                          javax.swing.UIManager.setLookAndFeel(info.getClassName());
                          break;
                      }
                  }
              } catch (ClassNotFoundException ex) {
                  java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
              } catch (InstantiationException ex) {
                  java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
              } catch (IllegalAccessException ex) {
                  java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
              } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                  java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
              }
              //</editor-fold>

              /* Create and display the form */
              java.awt.EventQueue.invokeLater(new Runnable() {
                  public void run() {
                      new NewJFrame().setVisible(true);
                  }
              });
          }

          // Variables declaration - do not modify
          // End of variables declaration
      }

        Attachments

          Activity

            People

            • Assignee:
              anthony Anthony Petrov (Inactive)
              Reporter:
              dcirujanojfx Diego Cirujano (Inactive)
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Imported: