无法将图标的图像从JLabel传输到另一个

时间:2012-03-16 12:09:31

标签: java swing drag-and-drop icons

我想从jlabel上的图标拖动图像,将其放在第二个jlabel上的另一个图标上。问题是拖放Transferhandler方法的某些实现似乎没有被调用,我无法从第一个jlabel的图标中检索图像。希望您能在我的代码中找到问题。

class dndHandler extends TransferHandler implements Transferable {

    private final DataFlavor flavors[] = { DataFlavor.imageFlavor };

    private Image image;

        @Override
        public int getSourceActions(JComponent c) {
            System.out.println("test"); // Does not work
            return TransferHandler.COPY;
        }

        @Override
        public Transferable createTransferable(JComponent comp) {
            System.out.println("test"); // Does not work
             image = null;
             image = (Image)((ImageIcon) (((JLabel)comp).getIcon())).getImage(); //We transfer image //image stays null
             return this;
        }

        @Override
        public boolean canImport(JComponent comp, DataFlavor flavor[]) {
            if (!(comp instanceof JLabel)) {
                return false;
            }
            System.out.println("test"); //It works here
            return true;
        }

        @Override
        public boolean importData(JComponent comp, Transferable t) {
            if (comp instanceof JLabel) {

                    System.out.println(((JLabel)comp).getName()); //It works but the image is null
                    ((JLabel)comp).setIcon(new ImageIcon(image)); //The label stays empty
                    return true;
            } 
            return false;
        }

        @Override
        public Object getTransferData(DataFlavor flavor) {
            if (isDataFlavorSupported(flavor)) {
                return image;
            }
            return null;
        }

        @Override
        public DataFlavor[] getTransferDataFlavors() {
            return flavors;
        }

        @Override
        public boolean isDataFlavorSupported(DataFlavor flavor) {
            return flavors[0].equals(flavor);
        }

    }

    MouseListener specialListener = new SpecialDragMouseAdapter();

    class SpecialDragMouseAdapter extends MouseAdapter {
        @Override
        public void mousePressed(MouseEvent evt) {
            JComponent comp = (JComponent)evt.getSource();
            TransferHandler th = comp.getTransferHandler();

            // Start the drag operation
            th.exportAsDrag(comp, evt, TransferHandler.COPY);
        }

    }

public class PrincipalFrame extends javax.swing.JFrame {

    TransferHandler dropHandler = new dndHandler();

    public PrincipalFrame()
    {
        JLabel label1 = new JLabel();
        JLabel label2 = new JLabel();

        label1.setName("label "+i+"-"+j);
        label1.setSize(50,50);
        label1.setBorder(BorderFactory.createLineBorder(java.awt.Color.black));
        label1.setIcon(new ImageIcon("put a file path on your computer"))
        label1.addMouseListener(specialListener);
        label1.setTransferHandler(dropHandler);

        label2.setName("label "+i+"-"+j);
        label2.setSize(50,50);
        label2.setBorder(BorderFactory.createLineBorder(java.awt.Color.black));
        label2.addMouseListener(specialListener);
        label2.setTransferHandler(dropHandler);
  }
}

public static void main(String args[]) {

    /*
     * Create and display the form
     */
    java.awt.EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            javax.swing.JFrame principalFrame = new PrincipalFrame();
            //Size of the window
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            principalFrame.setSize(Toolkit.getDefaultToolkit().getScreenSize().width,
                                   Toolkit.getDefaultToolkit().getScreenSize().height-40);
            //Center the window
            principalFrame.setLocation(0,0);
            principalFrame.setVisible(true); 
        }
    });
}

1 个答案:

答案 0 :(得分:1)

对不起,我只是志愿者,

一个错误

enter image description here

第二次错误

enter image description here

第三个错误

enter image description here

然后请阅读有关Drag and Drop的教程,示例here