/* * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. */ package dragdonedoesnotcome; import javafx.application.Application; import javafx.event.EventHandler; import javafx.geometry.Orientation; import javafx.scene.*; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.input.MouseEvent; import javafx.scene.layout.FlowPane; import javafx.stage.Stage; import javafx.scene.input.*; import java.awt.Dimension; import java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JApplet; import javax.swing.UIManager; import javax.swing.SwingUtilities; import javafx.embed.swing.JFXPanel; import javafx.application.Platform; /** * * @author asakharu */ public class DragDoneDoesNotCome extends Application { static final int PANEL_WIDTH_INT = 675; static final int PANEL_HEIGHT_INT = 400; @Override public void start(Stage primaryStage) { primaryStage.setTitle("no DRAG_DONE"); primaryStage.setScene(createScene()); primaryStage.show(); } static Scene createScene() { final Label dragLabel = new Label("Drag me"); final Button dragButton = new Button("Drag me"); final TextField dragText = new TextField("Drag me"); final TextField dropText = new TextField("Drop here"); setOnDragDetected(dragLabel); setOnDragDetected(dragButton); setOnDragDetected(dragText); setOnDragDone(dragLabel); setOnDragDone(dragButton); setOnDragDone(dragText); dropText.setOnDragOver(new EventHandler() { @Override public void handle(DragEvent t) { t.acceptTransferModes(TransferMode.ANY); } }); dropText.setOnDragDropped(new EventHandler() { @Override public void handle(DragEvent t) { Dragboard db = t.getDragboard(); boolean success = false; if (db.hasString()) { dropText.setText(db.getString()); success = true; } t.setDropCompleted(success); } }); FlowPane root = new FlowPane(Orientation.VERTICAL); root.getChildren().addAll(dragLabel, dragButton, dragText, dropText); return new Scene(root); } private static void setOnDragDetected(final Node node) { node.setOnDragDetected(new EventHandler() { @Override public void handle(MouseEvent t) { Dragboard db = node.startDragAndDrop(TransferMode.ANY); ClipboardContent content = new ClipboardContent(); content.putString(node.getClass().getSimpleName()); db.setContent(content); t.consume(); } }); } private static void setOnDragDone(final Node node) { node.setOnDragDone(new EventHandler() { @Override public void handle(DragEvent t) { System.out.println("Drag done for " + node.getClass().getSimpleName()); } }); } public static void main(String[] args) { if (args.length >= 1 && args[0].equals("-interop")) { iniJFXPanel(); } else { Application.launch(args); } } static void iniJFXPanel() { SwingUtilities.invokeLater(new Runnable() { public void run() { try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); } catch (Exception e) {} JFrame frame = new JFrame("no DRAG_DONE in JFXPanel"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JApplet applet = new SwingInterop(); applet.init(); frame.setContentPane(applet.getContentPane()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); applet.start(); } }); } } class SwingInterop extends JApplet { @Override public void init() { final JFXPanel fxPanel = new JFXPanel(); fxPanel.setPreferredSize(new Dimension(DragDoneDoesNotCome.PANEL_WIDTH_INT, DragDoneDoesNotCome.PANEL_HEIGHT_INT)); add(fxPanel, BorderLayout.CENTER); // create JavaFX scene Platform.runLater(new Runnable() { public void run() { fxPanel.setScene(DragDoneDoesNotCome.createScene()); } }); } }