title: Drag and Drop file path to Java Swing JTextField
body: <p>Using <a href="https://stackoverflow.com/questions/811248/how-can-i-use-drag-and-drop-in-swing-to-get-file-path">this question</a>, I created the class below, which handles drag and drop of files to a JTextField.  The point of the application is to be able to drag a file into the text field, and have the text field's text set to the file's path (you can see the goal in the code pretty clearly).</p>
  
  <p>My problem is the below code does not compile.  The compilation error states <code>Cannot refer to non-final variable myPanel inside an inner class defined in a different method</code>.  I haven't worked much with inner classes, so can seomeone show me how to resolve the error and get the code to behave as designed?</p>
  
  <p>Code:</p>
  
  <pre><code>import java.awt.datatransfer.DataFlavor;
  import java.awt.dnd.DnDConstants;
  import java.awt.dnd.DropTarget;
  import java.awt.dnd.DropTargetDropEvent;
  import java.io.File;
  import java.util.List;
  
  import javax.swing.*;
  
  public class Test {
  
  public static void main(String[] args) {
      JTextArea myPanel = new JTextArea();
  
      myPanel.setDropTarget(new DropTarget() {
          public synchronized void drop(DropTargetDropEvent evt) {
              try {
                  evt.acceptDrop(DnDConstants.ACTION_COPY);
                  List&lt;File&gt; droppedFiles = (List&lt;File&gt;) evt
                          .getTransferable().getTransferData(
                                  DataFlavor.javaFileListFlavor);
                  for (File file : droppedFiles) {
                      /*
                       * NOTE:
                       *  When I change this to a println,
                       *  it prints the correct path
                       */
                      myPanel.setText(file.getAbsolutePath());
                  }
              } catch (Exception ex) {
                  ex.printStackTrace();
              }
          }
      });
  
      JFrame frame = new JFrame();
      frame.add(myPanel);
      frame.setVisible(true);
  
  }
  
  }
  </code></pre>