I'm working on a JavaFX application that should interact with an existing Swing application via drag-and-drop. The data exchange via drag-and-drop actually works, but we want to rework that part of the functionality to actually exchange custom Java objects instead of simple Strings with objects serialized to JSON. The problem is, that the Swing UI does not receive the dragged data, if custom MIME types are used instead of e.g. text/plain
. Below you can find a minimal example for both the drag application (JavaFX) and the drop application (Swing).
FxDrag
public class FxDrag extends Application {
private static final DataFormat format = new DataFormat("application/x-my-mime-type");
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
BorderPane root = new BorderPane();
root.setOnDragDetected(event -> {
Dragboard dragboard = root.startDragAndDrop(TransferMode.COPY);
ClipboardContent content = new ClipboardContent();
content.putString("Test");
// content.put(format, "Test");
dragboard.setContent(content);
event.consume();
});
stage.setScene(new Scene(root, 300, 300));
stage.setTitle("Drag");
stage.show();
}
}
SwingDrop
public class SwingDrop {
public static void main(String[] args) {
new SwingDrop().run();
}
private void run() {
JPanel panel = new JPanel();
panel.setTransferHandler(new TransferHandler() {
@Override
public boolean canImport(TransferSupport support) {
return true;
}
@Override
public boolean importData(TransferSupport support) {
Stream.of(support.getDataFlavors()).forEach(flavor -> {
System.out.println(flavor.getMimeType());
});
return super.importData(support);
}
});
JFrame frame = new JFrame();
frame.setTitle("Drop");
frame.add(panel);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
When putting a String
via putString
to the content
in the JavaFX application, the Swing application receives the drag and provides the following flavors:
application/x-java-serialized-object; class=java.lang.String
text/plain; class=java.io.Reader; charset=Unicode
text/plain; class=java.lang.String; charset=Unicode
text/plain; class=java.nio.CharBuffer; charset=Unicode
text/plain; class="[C"; charset=Unicode
text/plain; class=java.io.InputStream; charset=unicode
text/plain; class=java.nio.ByteBuffer; charset=UTF-16
text/plain; class="[B"; charset=UTF-16
text/plain; class=java.io.InputStream; charset=UTF-8
text/plain; class=java.nio.ByteBuffer; charset=UTF-8
text/plain; class="[B"; charset=UTF-8
text/plain; class=java.io.InputStream; charset=UTF-16BE
text/plain; class=java.nio.ByteBuffer; charset=UTF-16BE
text/plain; class="[B"; charset=UTF-16BE
text/plain; class=java.io.InputStream; charset=UTF-16LE
text/plain; class=java.nio.ByteBuffer; charset=UTF-16LE
text/plain; class="[B"; charset=UTF-16LE
text/plain; class=java.io.InputStream; charset=ISO-8859-1
text/plain; class=java.nio.ByteBuffer; charset=ISO-8859-1
text/plain; class="[B"; charset=ISO-8859-1
text/plain; class=java.io.InputStream; charset=windows-1252
text/plain; class=java.io.InputStream
text/plain; class=java.nio.ByteBuffer; charset=windows-1252
text/plain; class="[B"; charset=windows-1252
text/plain; class=java.io.InputStream; charset=US-ASCII
text/plain; class=java.nio.ByteBuffer; charset=US-ASCII
text/plain; class="[B"; charset=US-ASCII
I can even drop different data from various applications like browsers etc. and the Swing application provides the respective data flavors in the drop (text, images etc.).
However, if I use my custom format, no flavors are listed at all. Does Swing filter the data flavors transfered via a drag-and-drop application?