请考虑以下代码:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.TransferHandler;
import javax.swing.TransferHandler.TransferSupport;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
class Element {
String s;
public Element(String s) {
this.s = s;
}
}
public class DnDTest {
public static void main(String args[]) {
JFrame f = new JFrame("Test");
f.setSize(640, 400);
Container c = f.getContentPane();
c.setLayout(new BorderLayout());
JTree t = new JTree();
t.setDragEnabled(true);
DefaultMutableTreeNode root = new DefaultMutableTreeNode(new Element("root"));
root.add(new DefaultMutableTreeNode(new Element("child")));
t.setModel(new DefaultTreeModel(root));
c.add(t, BorderLayout.WEST);
JPanel p = new JPanel();
c.add(p, BorderLayout.CENTER);
p.setTransferHandler(new TransferHandler("entity") {
@Override
public boolean canImport(TransferSupport support) {
return true;
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
Transferable t = support.getTransferable();
DataFlavor[] fv = t.getTransferDataFlavors();
Object o;
try {
for (DataFlavor f: fv) {
o = t.getTransferData(f);
System.out.println(f + " => " + o.getClass().getName()+", " + o);
}
} catch (UnsupportedFlavorException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
});
f.setVisible(true);
}
}
当我将节点从树中拖动到右侧面板时,会打印以下输出:
java.awt.datatransfer.DataFlavor[mimetype=text/html;representationclass=java.lang.String] => java.lang.String, <html>
<body>
<ul>
<li>Element@4d405ef7
</ul>
</body>
</html>
java.awt.datatransfer.DataFlavor[mimetype=text/html;representationclass=java.io.Reader] => java.io.StringReader, java.io.StringReader@217d12b3
java.awt.datatransfer.DataFlavor[mimetype=text/html;representationclass=java.io.InputStream;charset=unicode] => java.io.ByteArrayInputStream, java.io.ByteArrayInputStream@5c9992b3
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.lang.String] => java.lang.String, Element@4d405ef7
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.io.Reader] => java.io.StringReader, java.io.StringReader@34715be
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.io.InputStream;charset=unicode] => java.io.ByteArrayInputStream, java.io.ByteArrayInputStream@325e55cd
java.awt.datatransfer.DataFlavor[mimetype=application/x-java-jvm-local-objectref;representationclass=java.lang.String] => java.lang.String, Element@4d405ef7
java.awt.datatransfer.DataFlavor[mimetype=application/x-java-serialized-object;representationclass=java.lang.String] => java.lang.String, Element@4d405ef7
因此,无论我使用哪个DataFlavor
,结果始终为String
。我需要更改以实际获取对作为树节点的用户对象关联的Element
实例的引用?
答案 0 :(得分:0)
我做了一些挖掘,并找到了解决方案。发布供参考:
flatMap