2

我需要在我的应用程序中添加对从其他应用程序(文件管理器、Web 浏览器、MS Word 和 OO 编写器)拖放图像的支持到 JLabel。
JVM 中有很多关于 DnD 的示例,但我找不到任何可以从本机应用程序或其他 J2SE 应用程序中拖动的内容。

如何为文件管理器、Web 浏览器、MS Word 和 OO 编写器添加对图像的拖放支持?

谢谢

4

1 回答 1

4

沿着这些思路:

JComponent dropTargetComponent = ...;
new DropTarget(dropTargetComponent , DnDConstants.ACTION_COPY,
    new DropTargetListener() {

        public void drop(DropTargetDropEvent dtde) {
            Transferable tr = dtde.getTransferable();
            if(tr.isDataFlavorSupported(java.awt.datatransfer.DataFlavor.imageFlavor)) {
                 Image image = (Image) tr.getTransferData(java.awt.datatransfer.DataFlavor.imageFlavor);
                 // Do something with image
            }
         }

         ...
   }

您可能还想尝试java.awt.datatransfer.DataFlavor.javaFileListFlavor返回 a List<File>,因为来自 FileManager 的东西通常是文件列表。

这是一个完整的示例(从 Explorer 文件管理器和 FireFox 尝试 DnD):

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.net.URI;
import java.util.Iterator;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class TestNativeDnD {
    protected void initUI() {
        final JFrame frame = new JFrame(TestNativeDnD.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JLabel dropTargetComponent = new JLabel("Drop something here") {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(600, 600);
            }
        };
        dropTargetComponent.setHorizontalAlignment(JLabel.CENTER);
        final JLabel testLabel = new JLabel();
        new DropTarget(dropTargetComponent, DnDConstants.ACTION_COPY, new DropTargetListener() {

            @Override
            public void drop(DropTargetDropEvent dtde) {
                try {
                    Transferable tr = dtde.getTransferable();
                    dtde.acceptDrop(DnDConstants.ACTION_COPY);
                    if (tr.isDataFlavorSupported(java.awt.datatransfer.DataFlavor.imageFlavor)) {
                        Image image = (Image) tr.getTransferData(java.awt.datatransfer.DataFlavor.imageFlavor);
                        showImageIcon(new ImageIcon(image));
                    } else if (tr.isDataFlavorSupported(java.awt.datatransfer.DataFlavor.javaFileListFlavor)) {
                        List<File> fileList = (List<File>) tr.getTransferData(java.awt.datatransfer.DataFlavor.javaFileListFlavor);
                        Iterator<File> iterator = fileList.iterator();
                        if (iterator.hasNext()) {
                            File file = iterator.next();
                            showImageIcon(new ImageIcon(file.getAbsolutePath()));
                        }
                    } else {
                        DataFlavor[] flavors = tr.getTransferDataFlavors();
                        boolean handled = false;
                        for (int i = 0; i < flavors.length; i++) {
                            if (flavors[i].isRepresentationClassReader()) {
                                Reader reader = flavors[i].getReaderForText(tr);
                                BufferedReader br = new BufferedReader(reader);
                                java.lang.String line = null;
                                while ((line = br.readLine()) != null) {
                                    try {
                                        // kde seems to append a 0 char to the end of the reader
                                        if ("0".equals(line)) {
                                            continue;
                                        }
                                        File file = new File(new URI(line));
                                        showImageIcon(new ImageIcon(file.getAbsolutePath()));
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }
                    }
                } catch (UnsupportedFlavorException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            public void showImageIcon(ImageIcon icon) {
                testLabel.setIcon(icon);
                frame.pack();
            }

            @Override
            public void dragEnter(DropTargetDragEvent dtde) {
                // TODO Auto-generated method stub

            }

            @Override
            public void dragOver(DropTargetDragEvent dtde) {
                // TODO Auto-generated method stub

            }

            @Override
            public void dropActionChanged(DropTargetDragEvent dtde) {
                // TODO Auto-generated method stub

            }

            @Override
            public void dragExit(DropTargetEvent dte) {
                // TODO Auto-generated method stub

            }

        });
        frame.add(dropTargetComponent);
        frame.add(testLabel, BorderLayout.EAST);
        frame.setSize(600, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TestNativeDnD().initUI();
            }
        });
    }

}
于 2013-01-02T13:46:50.040 回答