I am trying to drag and drop text from Google Chrome into a Java Swing application on Linux, but the drop event is not triggered in my Java application. However, the same drag-and-drop operation works fine in Firefox.
Observations:
- Dragging from Chrome to gedit works fine.
- Dragging from Chrome to a Java app on Windows works as expected.
- No exception is thrown in the Java application, and
drop()
is never called.
Environment:
$DISPLAY
Output: :0.11
(X11 is in use).
$XDG_SESSION_TYPE
Output: (No value returned).
I tried forcing X11 mode by running:
export XDG_SESSION_TYPE=x11
but the issue persists.
Minimal Java Swing drop handler code
import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.dnd.*;
public class DragDropSwingApp {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("Drag & Drop Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
JTextArea textArea = new JTextArea("Drop text here...");
textArea.setFont(new Font("Arial", Font.PLAIN, 16));
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setDropTarget(new DropTarget() {
@Override
public void drop(DropTargetDropEvent dtde) {
try {
dtde.acceptDrop(DnDConstants.ACTION_COPY);
Object data = dtde.getTransferable().getTransferData(DataFlavor.stringFlavor);
textArea.setText(data.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
});
frame.add(new JScrollPane(textArea), BorderLayout.CENTER);
frame.setVisible(true);
});
}
}
Things I have checked:
- Java drop method never called:
drop()
is not triggered when dragging from Chrome, but it works with Firefox.
- Clipboard handling & X11/Wayland differences:
- Dragging works in other applications like gedit.
- Installed
xclip
clipboard on Red Hat 8, but the issue persists.
- JCEF integration & Chromium conflicts:
- I have Chromium installed separately for JCEF, but this shouldn't affect Java Swing drag-and-drop behavior.
Question:
Why does drag-and-drop from Chrome to Java Swing fail while it works with Firefox?
I guess this might be related to TransferHandler.canImport()
not being called from Chromium in Ubuntu (JDK-8299842) in some way.
Are there any workarounds or Java settings to fix this issue?
I am trying to drag and drop text from Google Chrome into a Java Swing application on Linux, but the drop event is not triggered in my Java application. However, the same drag-and-drop operation works fine in Firefox.
Observations:
- Dragging from Chrome to gedit works fine.
- Dragging from Chrome to a Java app on Windows works as expected.
- No exception is thrown in the Java application, and
drop()
is never called.
Environment:
$DISPLAY
Output: :0.11
(X11 is in use).
$XDG_SESSION_TYPE
Output: (No value returned).
I tried forcing X11 mode by running:
export XDG_SESSION_TYPE=x11
but the issue persists.
Minimal Java Swing drop handler code
import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.dnd.*;
public class DragDropSwingApp {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("Drag & Drop Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
JTextArea textArea = new JTextArea("Drop text here...");
textArea.setFont(new Font("Arial", Font.PLAIN, 16));
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setDropTarget(new DropTarget() {
@Override
public void drop(DropTargetDropEvent dtde) {
try {
dtde.acceptDrop(DnDConstants.ACTION_COPY);
Object data = dtde.getTransferable().getTransferData(DataFlavor.stringFlavor);
textArea.setText(data.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
});
frame.add(new JScrollPane(textArea), BorderLayout.CENTER);
frame.setVisible(true);
});
}
}
Things I have checked:
- Java drop method never called:
drop()
is not triggered when dragging from Chrome, but it works with Firefox.
- Clipboard handling & X11/Wayland differences:
- Dragging works in other applications like gedit.
- Installed
xclip
clipboard on Red Hat 8, but the issue persists.
- JCEF integration & Chromium conflicts:
- I have Chromium installed separately for JCEF, but this shouldn't affect Java Swing drag-and-drop behavior.
Question:
Why does drag-and-drop from Chrome to Java Swing fail while it works with Firefox?
I guess this might be related to TransferHandler.canImport()
not being called from Chromium in Ubuntu (JDK-8299842) in some way.
Are there any workarounds or Java settings to fix this issue?
1 Answer
Reset to default 0It is unusual to subclass DropTarget
. I don’t know if that can be expected to work. In particular, you did not set a component on the DropTarget instance. The usual way to use the DropTarget class is to instantiate it:
textArea.setDropTarget(
new DropTarget(textArea, DnDConstants.ACTION_COPY,
new DropTargetAdapter() {
@Override
public void drop(DropTargetDropEvent) {
// ...
}
}));
I’m not sure if this the cause of your issue, but… DataFlavor.stringFlavor does not correspond to the text/plain MIME type:
mimeType = "application/x-java-serialized-object"
The best way to receive text is with selectBestTextFlavor and getReaderForText:
@Override
public void drop(DropTargetDropEvent dtde) {
try {
dtde.acceptDrop(DnDConstants.ACTION_COPY);
DataFlavor textFlavor =
DataFlavor.selectBestTextFlavor(dtde.getCurrentDataFlavors());
if (textFlavor != null) {
StringWriter data = new StringWriter();
try (Reader dataReader =
textFlavor.getReaderForText(dtde.getTransferable())) {
dataReader.transferTo(data);
}
textArea.setText(data.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
You also might be able to make it work using getTextPlainUnicodeFlavor(), which does correspond to text/plain, but it returns the data as a raw InputStream, which means you would have to obtain the native charset (which usually is not the same as Charset.defaultCharset()!), then convert the InputStream to a Reader in order to get the correct text. It’s easier to use the above approach.