Is there any way to reach drop moment in Drag and Drop?

旧街凉风 提交于 2019-12-10 23:46:59

问题


I am making a java applet that transfer files from website to my desktop with Drag and Drop.I works but,Is there any way to learn when the dropping begins?I'm using MouseListener for listining mouse acts and using my transferhandler class for transfering data.

Code below like this..

public class DragApplet extends JApplet
{       
    public void init()
    {       
        Container contentPane = getContentPane();

        Image image = ...;
        JLabel label = new JLabel(new ImageIcon(image));
        label.setTransferHandler(new ImageSelection());

        MouseListener listener = new MouseAdapter() 
        {
            public void mousePressed(MouseEvent me) {

                    JComponent comp = (JComponent) me.getSource();
                    TransferHandler handler = comp.getTransferHandler();
                    handler.exportAsDrag(comp, me, TransferHandler.COPY);
            }
        }       
        label.addMouseListener(listener);
        contentPane.add(label,BorderLayout.CENTER);
    }
}

class ImageSelection extends TransferHandler implements Transferable 
{
    private static final DataFlavor flavors[] = { DataFlavor.javaFileListFlavor };

    public ImageSelection(){ // bla bla.. }

    public int getSourceActions(JComponent c) {
        // bla bla..
        return TransferHandler.COPY;
    }     

    public Transferable createTransferable(JComponent comp) {
        // bla bla..
        return this;
    }

    public DataFlavor[] getTransferDataFlavors() {
        // bla bla..      
        return flavors;
    }

    public boolean canImport(JComponent comp, DataFlavor flavor[]) {
        // bla bla..
        return true;
    }    

    public Object getTransferData(DataFlavor flavor){   
        //Download starts and save "C:\\Temp\\aa.mpg" here..
        String[] ary= {"C:\\Temp\\aa.mpg\\"};           
        return Arrays.asList(ary);              
    }     

    public boolean importData(JComponent comp, Transferable t) {
        // bla bla..
        return false;
    }    

    public boolean isDataFlavorSupported(DataFlavor flavor) {
        // bla bla..
        return true;
    }
}

回答1:


I do not know how you create your Transferable since it is not included in the question. In the drag-and-drop process the Transferable is created as soon as you start dragging. However, there is no need to start your download in the constructor of your Transferable.

You can start downloading when the Transferable#getTransferData method is called for the specific DataFlavor, which is, unless the DropTarget is badly implemented, only called when the drop has occurred.



来源:https://stackoverflow.com/questions/9128696/is-there-any-way-to-reach-drop-moment-in-drag-and-drop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!