In a Swing embedded JavaFx application, during drag and drop gesture, the mouse cursor does not show the correct allowed or forbidden state.
Please run the test program:
=====================
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
public class DndJavaFxSwing {
private void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Dnd JavaFX with Swing");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setPreferredSize(new Dimension(600, 400));
final JFXPanel fxMainPanel = new JFXPanel();
mainPanel.add(fxMainPanel, "0,0");
// create JavaFX scene
Platform.runLater(() -> createScene(fxMainPanel));
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setVisible(true);
}
private void createScene(JFXPanel fxMainPanel) {
Pane root = new HBox();
Pane dndSource = new StackPane();
dndSource.setPrefSize(200, 200);
dndSource.setStyle("-fx-background-color: blue");
dndSource.setOnDragDetected(event -> {
Dragboard db = dndSource.startDragAndDrop(TransferMode.COPY);
ClipboardContent content = new ClipboardContent();
content.putString("test");
db.setContent(content);
event.consume();
});
Pane dndTargetAllowed = new StackPane();
dndTargetAllowed.setPrefSize(200, 200);
dndTargetAllowed.setStyle("-fx-background-color: green");
dndTargetAllowed.setOnDragOver(event -> {
System.out.println("Drag over allowed");
event.acceptTransferModes(TransferMode.COPY);
event.consume();
});
Pane dndTargetForbidden = new StackPane();
dndTargetForbidden.setPrefSize(200, 200);
dndTargetForbidden.setStyle("-fx-background-color: red");
dndTargetForbidden.setOnDragOver(event -> {
System.out.println("Drag over forbidden.");
event.acceptTransferModes(TransferMode.MOVE);
event.consume();
});
root.getChildren().addAll(dndSource, dndTargetAllowed, dndTargetForbidden);
fxMainPanel.setScene(new Scene(root, 600, 400));
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
DndJavaFxSwing swingView = new DndJavaFxSwing();
swingView.createAndShowGUI();
});
}
}
=============================
Behaviour of the DnD:
Drag source:
No document symbol is displayed over the DnD source, but only an empty grey rectangle -> Document symbol missing?
Drag target (allowed):
Allowed symbol (green plus sign) is displayed only for a very short time interval (when the mouse enters the target pane) and thereafter it disappears.
Sometimes it appears again occasionally, it seems to appear on mouse entered event -> Green plus should be displayed al the time when the mouse is dragged over the allowed pane.
Drag target (forbidden):
Forbidden symbol is also displayed only for a very short time interval (when the mouse enters the target pane) and thereafter it disappears.
Sometimes it appears again occasionally, it seems to appear on mouse entered event -> Expected behaviour: to see the forbidden sign all the time when the mouse is dragged over the forbidden pane.
Please run the test program:
=====================
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
public class DndJavaFxSwing {
private void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Dnd JavaFX with Swing");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setPreferredSize(new Dimension(600, 400));
final JFXPanel fxMainPanel = new JFXPanel();
mainPanel.add(fxMainPanel, "0,0");
// create JavaFX scene
Platform.runLater(() -> createScene(fxMainPanel));
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setVisible(true);
}
private void createScene(JFXPanel fxMainPanel) {
Pane root = new HBox();
Pane dndSource = new StackPane();
dndSource.setPrefSize(200, 200);
dndSource.setStyle("-fx-background-color: blue");
dndSource.setOnDragDetected(event -> {
Dragboard db = dndSource.startDragAndDrop(TransferMode.COPY);
ClipboardContent content = new ClipboardContent();
content.putString("test");
db.setContent(content);
event.consume();
});
Pane dndTargetAllowed = new StackPane();
dndTargetAllowed.setPrefSize(200, 200);
dndTargetAllowed.setStyle("-fx-background-color: green");
dndTargetAllowed.setOnDragOver(event -> {
System.out.println("Drag over allowed");
event.acceptTransferModes(TransferMode.COPY);
event.consume();
});
Pane dndTargetForbidden = new StackPane();
dndTargetForbidden.setPrefSize(200, 200);
dndTargetForbidden.setStyle("-fx-background-color: red");
dndTargetForbidden.setOnDragOver(event -> {
System.out.println("Drag over forbidden.");
event.acceptTransferModes(TransferMode.MOVE);
event.consume();
});
root.getChildren().addAll(dndSource, dndTargetAllowed, dndTargetForbidden);
fxMainPanel.setScene(new Scene(root, 600, 400));
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
DndJavaFxSwing swingView = new DndJavaFxSwing();
swingView.createAndShowGUI();
});
}
}
=============================
Behaviour of the DnD:
Drag source:
No document symbol is displayed over the DnD source, but only an empty grey rectangle -> Document symbol missing?
Drag target (allowed):
Allowed symbol (green plus sign) is displayed only for a very short time interval (when the mouse enters the target pane) and thereafter it disappears.
Sometimes it appears again occasionally, it seems to appear on mouse entered event -> Green plus should be displayed al the time when the mouse is dragged over the allowed pane.
Drag target (forbidden):
Forbidden symbol is also displayed only for a very short time interval (when the mouse enters the target pane) and thereafter it disappears.
Sometimes it appears again occasionally, it seems to appear on mouse entered event -> Expected behaviour: to see the forbidden sign all the time when the mouse is dragged over the forbidden pane.