Java Drag and Drop in OS X Reports Moving Instead of Copying

Update / Status: An error has been reported in Oracle and is currently still open / not resolved: https://bugs.openjdk.java.net/browse/JDK-8054325 p>

I found a strange error that only appears on Java 7 and 8 on OS X, not Java 6, nor Java 7 Windows.

Below is the extracted error, removing as much unnecessary code as possible. This leaves us with a small table and a button, where we can drag a cell from the table on top of the button.

In our production code, we use TransferHandler , which has COPY_OR_MOVE as the source action. We have two goals, one DropTarget accepts as MOVE , the other accepts as COPY . It looks like the JDK / JRE is broken in OS X because if DropTarget accepts as COPY , then the reports in TranferHandler were MOVE .

If we execute the code below in Java 6 (or Java 7 on Windows), it reports ( as expected ):

 not move is copy 

But when we run it in OS X Java 7 or Java 8, we get the following:

 is move not copy 

I tested and reported this to Oracle (error report has not yet been completed). But since it will take (loooong) until it is fixed, maybe someone has an idea on how to make a β€œclean” workaround?

I would prefer that the target of the binding is not tied to the original component, telling it the actual action (copy or move), this sound is too complicated.

 import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Transferable; import java.awt.dnd.DnDConstants; import java.awt.dnd.DropTarget; import java.awt.dnd.DropTargetAdapter; import java.awt.dnd.DropTargetDropEvent; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTable; import javax.swing.TransferHandler; import javax.swing.table.DefaultTableModel; public class DropExample extends JPanel { public static void main(final String[] args) { final JFrame frame = new JFrame(); frame.setSize(200, 100); frame.add(new DropExample()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public DropExample() { add(createTable()); final JButton button = new JButton("Drop here"); button.setDropTarget(new DropTarget(button, new DropTargetAdapter() { @Override public void drop(final DropTargetDropEvent dtde) { //Here we accept the drop, as COPY: dtde.acceptDrop(DnDConstants.ACTION_COPY); dtde.dropComplete(true); } })); add(button); } private JTable createTable() { final DefaultTableModel tm = new DefaultTableModel(); tm.addColumn("Column 0"); tm.addRow(new String[] { "Table 00" }); final JTable table = new JTable(tm); table.setDragEnabled(true); table.setTransferHandler(new TransferHandler() { private static final long serialVersionUID = 1L; @Override public int getSourceActions(final JComponent c) { return COPY_OR_MOVE; } @Override protected Transferable createTransferable(final JComponent c) { return new StringSelection(c.toString()); } @Override protected void exportDone(final JComponent source, final Transferable data, final int action) { System.out.println(((action & DnDConstants.ACTION_MOVE) > 0)?"is move":"not move"); System.out.println(((action & DnDConstants.ACTION_COPY) > 0)?"is copy":"not copy"); // On Java 7 it reports: "is move" and "not copy" // On Java 6 it (correctly) reports: "not move" and "is copy" } }); return table; } } 
+3
java java-7 drag-and-drop macos
source share

No one has answered this question yet.

See similar questions:

8
How to detect META key presses while dragging and dropping on OSX

or similar:

756
How to copy an object in Java?
595
Downloading Java JDK on Linux via wget is shown on the license page instead
195
How to drag and drop files into the application?
46
Tutorial on dragging an item from a UITableView to a UITableView
10
Drag and Drop Nodes in JTree
2
Can someone show me a simple working implementation of PagerSlidingTabStrip?
2
Continue JTable data to another JTable next to it instead of scrolling
2
How can I correctly calculate jTable line height to make sure that the HTML code contained in jLabel is fully displayed?
one
JTable will not show on JPanel
0
Subclass of Problem TreeNode DnD

All Articles