I have an application where I allow strings to be dragged and dropped on to one of the components. This works fine.
Now I want to be able to add customization options to this operation. My first attempt is to construct and show a JPopupMenu after the drop action has been accepted. This does not seem to work as the popup menu is not being displayed, it is like the show method is being ignored.
Is there any workaround for this behaviour, or will I have to use something heavier like a JOptionPane?
Here is my sample code:
import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetAdapter;
import java.awt.dnd.DropTargetDropEvent;
import static java.awt.dnd.DnDConstants.ACTION_COPY;
public class DNDTest {
DNDTest() {
JFrame f = new JFrame("Test dnd");
Container c = f.getContentPane();
final JPanel jp = new JPanel(new BorderLayout());
jp.setBackground(Color.RED);
jp.setToolTipText("Drop a string here");
jp.setPreferredSize(new Dimension(200,200));
c.add(jp);
DropTarget dt = new DropTarget(jp, ACTION_COPY, new DropTargetAdapter() {
@Override
public void drop(DropTargetDropEvent dtde) {
if (dtde.isDataFlavorSupported(DataFlavor.stringFlavor)) {
try {
dtde.acceptDrop(ACTION_COPY);
String trns = (String) dtde.getTransferable().getTransferData(DataFlavor.stringFlavor);
System.err.println("Dropped string "+trns);
Point loc = dtde.getLocation();
// Show a popup after the drop operation
JPopupMenu jpm2 = new JPopupMenu("Popup menu");
JMenuItem jmi2 = new JMenuItem("Popup option 1");
jpm2.add(jmi2);
System.err.println("About to show popup after drop at "+loc.getX()+","+loc.getY());
jpm2.show(jp, (int)loc.getX(), (int)loc.getY());
} catch (Exception ex) {
ex.printStackTrace();
}
return;
}
dtde.rejectDrop();
}
}, true);
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(DNDTest::new);
}
}