Hello friends i am working on an image loader in java which allow user to drag and drop folder of images into JTable and showing the thumbnails but drag and drop of folder takes too much time to load thumbnails in table cell ..

package org.tempuri.eyestudio.swg;
/*
* FileAndTextTransferHandler.java is used by the 1.4
*/


import java.io.*;
import java.net.URL;
import java.util.*;
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
class FileAndTextTransferHandler extends TransferHandler {
private DataFlavor fileFlavor, stringFlavor;
private ThumbnailImagesPanelNew tip;
private ImagesNamesPanel inp;
private JPanel source;
private boolean shouldRemove;
protected String newline = "\n";
int x=30,y=20,previousSize=0;
java.io.FileFilter fileFilter=new FileListFilter();
 /** Creates a new instance of FileAndTextTransferHandler */
public FileAndTextTransferHandler()
{       
   fileFlavor = DataFlavor.javaFileListFlavor;
   stringFlavor = DataFlavor.stringFlavor;
   x=30;
   y=30;
}
/**
 * Methode Name    :   setDropTraget()
 *  Parameter       :   Object
 *  Return Type     :   void
 *  Purpose         :   This method sets the drop trget
 */
public void setDropTraget(Object component)
{
    if(component instanceof ThumbnailImagesPanelNew)
    {
        System.out.println("Thumbnails");
        tip=(ThumbnailImagesPanelNew)component;
        source=tip.getPanel();
    }
    else
    {
        System.out.println("Names List");
        inp=(ImagesNamesPanel)component;
        source=(JPanel)inp;
    }
}
public boolean importData(JComponent c, Transferable t) 
{
    if (!canImport(c, t.getTransferDataFlavors())) 
    {
        return false;
    }
    try
    {
        if (hasFileFlavor(t.getTransferDataFlavors())) 
        {
            String str = null;
            java.util.List files =(java.util.List)t.getTransferData(fileFlavor);
            //Collections.sort(files, String.CASE_INSENSITIVE_ORDER);
            tip.storeLocalImages(files);
            java.util.List  fileList= getAllFiles(files);
            tip.storeLocalImages(fileList);
            tip.displayThumbnails();
            inp.addLocalImgPath(files);  
            System.out.println("<<<<<< Inside Method importData( >>>>>>>>>>>>>");
            return true; 
        }
    }
    catch(Exception ex)
    {
        ex.printStackTrace();
    }
  return false;
}
public int getSourceActions(JComponent c)
{
    return COPY_OR_MOVE;
}
public boolean canImport(JComponent c, DataFlavor[] flavors)
{
    if (hasFileFlavor(flavors))
    { 
        return true; 
    }
 return false;
}
private boolean hasFileFlavor(DataFlavor[] flavors)
{
    for (int i = 0; i < flavors.length; i++)
    {
        if (fileFlavor.equals(flavors[i]))
        {
            return true;
        }
    }
    return false;
}
private boolean hasStringFlavor(DataFlavor[] flavors)
{
    for (int i = 0; i < flavors.length; i++)
    {
        if (stringFlavor.equals(flavors[i]))
        {
            return true;
        }
    }
    return false;
}

public java.util.List getAllFiles(java.util.List fileList)
{
    Iterator it=fileList.iterator();
    java.util.ArrayList allDirFiles=null;
    java.util.List filesList=new ArrayList();
    while(it.hasNext())
    {
        File f=(File)it.next();
         System.out.println("File Name==="+f.getName());
        if(f.isDirectory())
        {
            filesList.add(f);// Directory Added
            allDirFiles=ListDirectory.getAllFilesList(f);
        }
        else
        {
            filesList.add(f);
        }
        if(allDirFiles!=null)
        {
            Iterator it2=allDirFiles.iterator();
            while(it2.hasNext())
            {
               filesList.add((File)it2.next());
            }
            it2=null;
        }
     ListDirectory.clearList();
    }

   return filesList;
}


public File[] directoryContent(File Dir)
{
    File []files=Dir.listFiles(fileFilter);
    for(int i=0;i<files.length;i++)
    {
        if(files[i].isDirectory())
        {
            directoryContent(files[i]);
        }
    }
    return files;
}



}

class ListDirectory
{
static int indentLevel = -1;
static ArrayList allFilesList=new ArrayList();
static void listPath(File path)
{
File files[];  // list of files in a directory
indentLevel++; // going down...
// Create list of files in this dir.
files = path.listFiles(new FileListFilter());
// Sort with help of Collections API.
Arrays.sort(files);
for (int i=0, n=files.length; i < n; i++)
{
  for (int indent=0; indent < indentLevel; indent++)
  {
    //System.out.print("    ");
  }
  if(!files[i].isDirectory())
  {
    ////System.out.println(files[i].getPath());
    allFilesList.add(files[i]);
  }
  if (files[i].isDirectory())
  {
    // Recursively descend dir tree.
    listPath(files[i]);
  }
}
indentLevel--; // And going up
}
public static ArrayList getAllFilesList(File path)
{
  listPath(path);
  return allFilesList;
}
public static void clearList()
{
  allFilesList.clear();:eek: 

}
}

Converting an image to a thumbnail does take time. I have one application where I need to process and display a bunch of images, I use a background thread, and update the window as the images are ready. That way the user at least still has control of the window, and could close it, if needed.

Thanks JeffHeaton actully i have already done that still my client needs fast processing of image drag and drop ., Do you have any other idea for that if you are online let me tell you what actully i have done and what i need ......

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.