直接调用dragFile方法,传入控件及OnDragFile监听器,当有文件拖到控件上会调用监听

      public void dragFile(Component c, OnDragFile onDragFile)
         {
            new DropTarget(c,DnDConstants.ACTION_COPY_OR_MOVE,new DropTargetAdapter()
             {
                @Override
                public void drop(DropTargetDropEvent dtde)
                {
                    try{

                        if(dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor))
                         {
                            dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
                            List<File>list=(List<File>)(dtde.getTransferable().getTransferData(DataFlavor.javaFileListFlavor));
                            if(onDragFile!=null){
                                onDragFile.onDragFile(list);
                            }
//                          String temp="";
//                          for(File file:list)
//                           {
//                              temp+=file.getAbsolutePath()+";\n";
//                              JOptionPane.showMessageDialog(null, temp);
//                              dtde.dropComplete(true);
//                              
//                           }

                         }

                        else
                         {

                            dtde.rejectDrop();
                         }

                    }catch(Exception e){e.printStackTrace();}

                }

             });

         }

      public interface OnDragFile{
          public void onDragFile(List<File> list_file);
      }

发表评论

邮箱地址不会被公开。 必填项已用*标注