java异常代码错误拖放

时间:2016-04-20 07:03:33

标签: java swing debugging drag-and-drop jtable

此代码中存在错误。代码不应该重写通过拖动获得的文件的路径(如果它已经存在)。请问你能帮帮我吗?我正在实施拖放操作。原始文件

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.util.List;
import java.util.StringTokenizer;


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);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    add(new JLabel("Drop a list from your file chooser 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("Source: " + dte.getSource());
//      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 {
          Transferable tr = dtde.getTransferable();
          DataFlavor[] flavors = tr.getTransferDataFlavors();
          for (int i = 0; i < flavors.length; i++) {
            System.out.println("Possible flavor: " + flavors[i].getMimeType());
            if (flavors[i].isFlavorJavaFileListType()) {
              dtde.acceptDrop(DnDConstants.ACTION_COPY);

             // ta.setText("Successful file list drop.\n\n");

              //System.out.println("---"+ta.getText());
              java.util.List list = (java.util.List) tr.getTransferData(flavors[i]);
              for (int j = 0; j < list.size(); j++) {
                  StringTokenizer st = new StringTokenizer(ta.getText(), "\n");
                  ta.append(list.get(j) + "\n");

              }

            }

              dtde.dropComplete(true);
              return;

          }
          System.out.println("Drop failed: " + dtde);
          dtde.rejectDrop();
        } catch (Exception e) {
          e.printStackTrace();
          dtde.rejectDrop();
        }
      }


  public static void main(String args[]) {
    new Main();
  }
}

1 个答案:

答案 0 :(得分:1)

这是方法的单个退出点可以帮助你...

让我们删除一些代码,仔细看看......

public void drop(DropTargetDropEvent dtde) {
    try {
        Transferable tr = dtde.getTransferable();
        DataFlavor[] flavors = tr.getTransferDataFlavors();
        for (int i = 0; i < flavors.length; i++) {
            System.out.println("Possible flavor: " + flavors[i].getMimeType());
            if (flavors[i].isFlavorJavaFileListType()) {
                //...
            }
            // !!!
            dtde.dropComplete(true);
            return;
            // !!!
        }
        System.out.println("Drop failed: " + dtde);
        dtde.rejectDrop();
    } catch (Exception e) {
        e.printStackTrace();
        dtde.rejectDrop();
    }
}

基本上,当您循环浏览所有可用的DataFlavors时,您会立即接受放置并在第一个放置return

因此,如果相反,我们将这些行移到if语句

public void drop(DropTargetDropEvent dtde) {
    try {
        Transferable tr = dtde.getTransferable();
        DataFlavor[] flavors = tr.getTransferDataFlavors();
        for (int i = 0; i < flavors.length; i++) {
            System.out.println("Possible flavor: " + flavors[i].getMimeType());
            if (flavors[i].isFlavorJavaFileListType()) {
                dtde.acceptDrop(DnDConstants.ACTION_COPY);

                // ta.setText("Successful file list drop.\n\n");
                //System.out.println("---"+ta.getText());
                java.util.List list = (java.util.List) tr.getTransferData(flavors[i]);
                for (int j = 0; j < list.size(); j++) {
                    StringTokenizer st = new StringTokenizer(ta.getText(), "\n");
                    ta.append(list.get(j) + "\n");

                }
                dtde.dropComplete(true);
                return;

            }

        }
        System.out.println("Drop failed: " + dtde);
        dtde.rejectDrop();
    } catch (Exception e) {
        e.printStackTrace();
        dtde.rejectDrop();
    }
}

它将开始工作。

然而,我们可以通过更好地利用可用功能来简化代码,例如...

public void drop(DropTargetDropEvent dtde) {
    try {
        Transferable tr = dtde.getTransferable();
        if (tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
            dtde.acceptDrop(DnDConstants.ACTION_COPY);
            List files = (List) tr.getTransferData(DataFlavor.javaFileListFlavor);
            for (Object obj : files) {
                ta.append(obj + "\n");
            }
            dtde.dropComplete(true);
        } else {
            System.out.println("Drop failed: " + dtde);
            dtde.rejectDrop();
        }
    } catch (Exception e) {
        e.printStackTrace();
        dtde.rejectDrop();
    }
}