title: Waiting for Drag and Drop gesture before executing a method
body: <p>I have a class called <code>FileDragDemo</code> that its purpose is to Drag and Drop (DnD) a PDF file to the GUI, and once this is done it shows the path of the file that was dropped. Let’s call this Step 1, which currently works just fine! I then want to save this file path as a string variable <code>StringFile</code> which I want to use and pass on to a different class <code>PdfEasyManager</code>.</p>
  
  <p>Step 2 is to run class <code>PdfEasyManager</code>(after the previous step is fully completed) and pass on the <code>StringFile</code> variable. This is where I am struggling- I created a method called <code>CallClass</code> which I want to execute after all of step 1 is completed (DnD and the String variable is stored); in other words, I want method <code>CallClass</code> to be the last step taken in the <code>FileDragDemo</code> class. When I place <code>CallClass</code> as the last item in the main method, the program executes it before DnD  gesture… in other words, the program does not wait for the user to DnD the file before it calls <code>PdfEasyManager</code>. I have also tried changing the location of <code>CallClass</code> but I get different errors. Sorry for all the comments in the code as I was trying different things..</p>
  
  <p><strong>Below is the Code for Filedragdemo:</strong></p>
  
  <pre><code>import java.awt.datatransfer.DataFlavor;
  import java.awt.datatransfer.UnsupportedFlavorException;
  import java.io.File;
  import java.io.IOException;
  import java.util.ArrayList;
  import java.util.List;
  import javax.swing.*;
  
  @SuppressWarnings("serial")
  public class FileDragDemo extends JPanel {
  
  
  public static String StringFile; 
  public JList list = new JList();
  
  
  
  public FileDragDemo() {
    list.setDragEnabled(true);
    list.setTransferHandler(new FileListTransferHandler(list));
  
    add(new JScrollPane(list));
  
  }
  
  public static void createAndShowGui() {
    FileDragDemo mainPanel = new FileDragDemo();
  
    JFrame frame = new JFrame("FileDragDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(mainPanel);
    frame.pack();
    frame.setLocationByPlatform(true);
    frame.setVisible(true);
  
  
  }
  
  public static void CallClass() throws Exception {
  
     PdfEasyManager PdfEasyManagerObject = new PdfEasyManager ();
  
        try {
  
            PdfEasyManagerObject.main(null);
  
          } catch (IOException e) {
               //TODO Auto-generated catch block
              e.printStackTrace();
          }
  }
  
  public static void main(String[] args) throws Exception {
  
  
    SwingUtilities.invokeLater(new Runnable() {
       public void run() {
          createAndShowGui();
  
       }
  
    }); 
  callClass();
  }
  
  }
  
  @SuppressWarnings("serial")
  class FileListTransferHandler extends TransferHandler {
  public JList list;
  
  
  public static String StringFile;
  
  
  public FileListTransferHandler(JList list) {
    this.list = list;
  }
  
  public int getSourceActions(JComponent c) {
    return COPY_OR_MOVE;
  }
  
  public boolean canImport(TransferSupport ts) {
    return ts.isDataFlavorSupported(DataFlavor.javaFileListFlavor);
  }
  
  public boolean importData(TransferSupport ts){
    try {
       @SuppressWarnings("rawtypes")
  
       List data = (List) ts.getTransferable().getTransferData(
             DataFlavor.javaFileListFlavor);
       if (data.size() &lt; 1) {
          return false;
       }// close if
  
       DefaultListModel listModel = new DefaultListModel();
  
       for (Object item : data) {
  
            File file = (File) item;
           //file1 = item;
          listModel.addElement(file);
          System.out.println ("%%%%%%%%%%%file... " + file);
          StringFile = file.toString();
          System.out.println ("%%%%%%%%%%%string... " + StringFile);          
       } // close for
       //String filestring = file.toString();
  
       list.setModel(listModel);
       return true;
  
  
    }// close try  
    catch (UnsupportedFlavorException e) {
       return false;
    } catch (IOException e) {
       return false;
    } 
  
  }
  }
  </code></pre>
  
  <p><strong>Below is the Code for PdfEasyManager:</strong></p>
  
  <pre><code>import java.io.File;
  import java.io.IOException;
  
  
  public class PdfEasyManager {
  
  public static File file;  
  
  public static void main(String[] args) throws Exception {
  
  
      CallEasy();
      CallSearch();
  
      /* System.out.println ("StringFile is: " + StringFile);
      file = new File (StringFile);*/
  
      //EasySearch easysearchobject = new EasySearch();
      //System.out.println(easysearchobject.ToText()); //For some reason this needs to be there in order for it to work..?
  
      /*searchPdftext searchPdftextobject = new searchPdftext();
                  try {
                      searchPdftextobject.main(null);
          } catch (Exception e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
          }*/
  
  
   }
  public static void CallEasy()throws Exception {
      EasySearch easysearchobject = new EasySearch();
      System.out.println(easysearchobject.ToText()); //For some reason this needs to be there in order for it to work..?
  }
  
  public static void CallSearch()throws Exception {
       searchPdftext searchPdftextobject = new searchPdftext();
       try {
           searchPdftextobject.main(null);
       } catch (Exception e1) {
           // TODO Auto-generated catch block
           e1.printStackTrace();
       }
  }
  }    
  </code></pre>