【问题标题】:javafx drag and drop not able to drop in another application, can from a swing wrapperjavafx拖放不能放到另一个应用程序中,可以从swing wrapper
【发布时间】:2017-01-31 06:06:20
【问题描述】:

这是一个示例应用程序,它允许您将一些文本拖放到其他区域。

我想加入的一个应用程序允许从 swing 中删除,但不允许从 JavaFX 中删除。

如果 JavaFX 位于 JFXPanel 和 Swing JFrame 中,它会下降。 如果它只是一个 Stage 中的纯 JavaFX,它具有“禁止进入”标志,并且不接受丢弃。

其他应用程序允许或阻止两者。但这一个应用程序接受 JFrame 的 drop 而不是 Stage,实际的 Label 组件等是相同的。

import javafx.application.Application;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import javax.swing.*;

public class DragViewExample extends Application {

@Override
public void start(Stage primaryStage) {
    TextField tf = new TextField("Copied Text");

    tf.setOnDragDetected(e -> {
        Dragboard db = tf.startDragAndDrop(TransferMode.COPY);
        db.setDragView(new Text(tf.getText()).snapshot(null, null), e.getX(), e.getY());
        ClipboardContent cc = new ClipboardContent();
        cc.putString(tf.getText());
        db.setContent(cc);
    });

    tf.setOnDragDropped(event -> {
        System.out.println("dropped");
    });

    tf.setOnDragDone(event -> {
        System.out.println("done");
    });

    //FULL JAVAFX
    Scene scene = new Scene(new StackPane(new HBox(10, tf)), 350, 75);
    primaryStage.setScene(scene);
    primaryStage.show();

    //JAVAFX INSIDE SWING
//        JFrame frame = new JFrame();
//        JFXPanel fxPanel = new JFXPanel();
//        Scene swingscene = new Scene(new StackPane(new HBox(10, tf)), 350, 75);
//        fxPanel.setScene(swingscene);
//        frame.setContentPane(fxPanel);
//        frame.pack();
//        frame.setVisible(true);
}

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

【问题讨论】:

    标签: java swing javafx drag-and-drop


    【解决方案1】:

    它不起作用,因为您还没有在onDragOver 上实现: 此处进行拖放的正确方法是(source 是 textField):

    source.setOnDragDetected(new EventHandler<MouseEvent>() {
        public void handle(MouseEvent event) {
            /* drag was detected, start a drag-and-drop gesture*/
            /* allow any transfer mode */
            Dragboard db = source.startDragAndDrop(TransferMode.ANY);
    
            /* Put a string on a dragboard */
            ClipboardContent content = new ClipboardContent();          
            content.putString(source.getText());
            db.setDragView(new Text(tf.getText()).snapshot(null, null), e.getX(), e.getY());
            db.setContent(content);
    
            event.consume();
        }
    });
    
    
    target.setOnDragOver(new EventHandler<DragEvent>() {
        public void handle(DragEvent event) {
            /* data is dragged over the target */
    
            if (event.getDragboard().hasString()) {
                /* allow for both copying and moving, whatever user chooses */
                event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
            }
    
            event.consume();
        }
    });
    

    完整的答案,请遵循官方 oracle 教程

    (http://docs.oracle.com/javafx/2/drag_drop/jfxpub-drag_drop.htm)

    【讨论】:

    • 您好,感谢您的快速回复。也许我在帖子中并不清楚,但目标不在我的应用程序中。这是第 3 方应用程序,它接受丢弃。所以我没有 setOnDragOver 的目标。第 3 部分应用程序从 JFXPanel/JFrame 接受它,但不从舞台接受它。
    • @user486415 如果您从 JavaFX 应用程序中正确执行代码,那么问题出在目标应用程序中。目标应用程序的代码在哪里?也许它不接受 TransferMode.Copy 。试试我添加的代码,让我知道会发生什么......
    • 我没有目标应用程序的代码。它接受上面代码示例的 TransferMode.COPY,我在 JFrame 中有它,但在舞台中时不接受。我看不出调试有什么真正的区别
    • @user486415 您使用的是什么操作系统?还可以将文本从舞台拖到文本编辑器中,例如notepad 可以吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-14
    • 1970-01-01
    • 1970-01-01
    • 2010-11-15
    • 2011-04-02
    • 2015-12-08
    相关资源
    最近更新 更多