将文本文件拖放到Java Applet中

时间:2013-12-16 09:57:06

标签: java applet

我正在开发一个桌面应用程序,我需要将文本文件拖放到JAVA applet上,该applet应该能够自动检测文件的绝对路径,并且应该能够执行某些操作。 (就像我的情况一样,在服务器上传)

我在接受该applet上的文件时遇到问题,该applet是一个JTextArea。

任何帮助以及代码和说明都会有所帮助。

先谢谢

以下是用于删除文件并显示其路径的代码。但问题是applet只接受来自File explorer的文件,而不是直接从桌面或任何其他窗口接收文件。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
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.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;

public class Main extends JFrame implements DropTargetListener {
  DropTarget dt;
  JTextArea ta = new JTextArea();
  public Main() {
    super("Drop Test");
    setSize(300, 300 );
    getContentPane()
        .add(new JLabel("Drop something here:"), BorderLayout.NORTH);

    ta.setBackground(Color.white);
    getContentPane().add(ta, BorderLayout.CENTER);
    dt = new DropTarget(ta, this);
    setVisible(true);
  }

 public void dragEnter(DropTargetDragEvent dtde) {
   // System.out.println("Drag Enter");
  }

  public void dragExit(DropTargetEvent dte) {
    //System.out.println("Drag Exit");
  }

  public void dragOver(DropTargetDragEvent dtde) {
    //System.out.println("Drag Over");
  }

  public void dropActionChanged(DropTargetDragEvent dtde) {
    System.out.println("Drop Action Changed");
  }

  public void drop(DropTargetDropEvent dtde) {
    try {
        System.out.println("in drop");
      Transferable tr = dtde.getTransferable();

      DataFlavor[] flavors = tr.getTransferDataFlavors();
      System.out.println(flavors.length);
      for (int i = 0; i < flavors.length; i++) {
        if (flavors[i].isFlavorJavaFileListType()) {
            System.out.println("In if");
          dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
          List list = (List) tr.getTransferData(flavors[i]);
          for (int j = 0; j < list.size(); j++) {
            ta.append(list.get(j) + "\n");
          }
          dtde.dropComplete(true);
          return;
        } else if (flavors[i].isFlavorSerializedObjectType()) {
          dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
          Object o = tr.getTransferData(flavors[i]);
          ta.append("Object: " + o.getClass());
          dtde.dropComplete(true);
          return;
        } else if (flavors[i].isRepresentationClassInputStream()) {
          dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
          ta.read(new InputStreamReader((InputStream) tr.getTransferData(flavors[i])),
              "from system clipboard");
          dtde.dropComplete(true);
          return;
        }
      }
      dtde.rejectDrop();
    } catch (Exception e) {
      e.printStackTrace();
      dtde.rejectDrop();
    }
  }
  public static void main(String args[]) {
    new Main();
  }
}

2 个答案:

答案 0 :(得分:0)

您必须在该applet中实现自己的Transferhandler(或者您的TextArea,在这两种情况下都可以设置自己的传输处理程序):

注意:这主要是从java拖放教程

中取得的1:1
public AppletTest() throws HeadlessException {
    super();
    TransferHandler tf = new TransferHandler(){
        @Override
        public boolean canImport(TransferSupport arg0) {

            for (DataFlavor df:  arg0.getDataFlavors() ){
                if (df.isFlavorJavaFileListType() ){
                    return true;
                }
            }
            //we only accept files
            return false;
        }

        @Override
        public boolean importData(TransferSupport arg0) {
            try {

                @SuppressWarnings("unchecked")
                List<File> oList = (List<File>) arg0.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
                for(File o: oList){
                    System.out.println("file:"+o);                      
                }
            } catch (UnsupportedFlavorException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return super.importData(arg0);
        }

    };
    this.setTransferHandler(tf);
}

一旦你有文件,其余的应该很容易......

答案 1 :(得分:0)

你需要将transferHandler设置为textArea然后(如上所述)......

private JtextArea ta;
public AppletTest() throws HeadlessException {
    super();
    TransferHandler tf = new TransferHandler(){
        //code as above
        @Override
        public boolean canImport(TransferSupport arg0) {

            for (DataFlavor df:  arg0.getDataFlavors() ){
                if (df.isFlavorJavaFileListType() ){
                     return true;
                }
            }
            //we only accept files
            return false;
        }


        @Override
        public boolean importData(TransferSupport arg0) {
            try {
            @SuppressWarnings("unchecked")
                List<File> oList = (List<File>) arg0.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
                 //check that you don't have multiple files selected
                if (oList.size() == 1){
                    File myFile = oList.get(0);
                    ta.setText(myFile.toString() );
                }
            } catch (UnsupportedFlavorException e) {
                e.printStackTrace();
            } catch (IOException e) {
            e.printStackTrace();
            }
            return super.importData(arg0);
        }
    };

    ta = new JTextArea();
    this.add(ta);   
    ta.setTransferHandler(tf);

}

我希望有帮助...

按要求:整个数据上下文:

public class AppletTest extends JApplet{

    private JTextArea ta;

    public AppletTest() throws HeadlessException {
        //as seen above
        ...
    }
}