Thursday, September 5, 2013

Drag and Drop

Sometimes, you may want to enable the drag and drop in your programs so they are more friendly. In this post, you are going to learn drag and drop task in Java. It is interesting when the user can select some files from other applications and place them in your applications.

In Java, the drag and drop can be performed by using the DropTarget (Component c, DropTargetListener dl). This tool attaches a component to a drop listener to handle the drop processing. The dragged and dropped data are stored in DataFlavor objects. You can get an array of DataFlavor  objects by using the getTransferDataFlavors() of the Tranferable object.  The DropTartgetDropEvent or DropTartgetDragEvent class can give you the Transferable object. To take the data in the DataFlavor object, you will use the getTransferData(DataFlavor) method of the Transferable object. The data can be text, image, or a list of files.

In the example below, the program allows the user to drag some image files  from the drive D to display in the JFrame window. The images are resized to 100X100 dimension before they are shown.

drag and drop java program


import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics2D;
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.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

class JLabelShow extends JFrame{
Container contpane;
JLabelShow(String title){
setTitle(title);
setSize(600,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contpane=getContentPane();
contpane.setLayout(new FlowLayout(FlowLayout.LEFT));
new DropTarget(contpane, new DropList());
setVisible(true);

}


class DropList implements DropTargetListener{

public void drop(DropTargetDropEvent e){
e.acceptDrop(DnDConstants.ACTION_COPY);
Transferable transfer=e.getTransferable();
DataFlavor[] df=transfer.getTransferDataFlavors();
for(int i=0;i<df.length;i++){
try{
Object x=transfer.getTransferData(df[i]);
if(x instanceof List){
List lst=(List)x;
int size=lst.size();
for(int count=0;count<size;count++){
File f=(File)lst.get(count);
String fName=f.getName();
if(fName.endsWith("png") || fName.endsWith("jpg") || fName.endsWith("gif") )
{


         BufferedImage img=resizedImage(f);
JLabel lbl=new JLabel(new ImageIcon(img));                          
        contpane.add(lbl);
validate();

}
}

}


}catch(Exception ep){ep.printStackTrace();}


}

e.dropComplete(true);

}
public void dropActionChanged(DropTargetDragEvent e){

}
public void dragOver(DropTargetDragEvent e){

}
public void dragExit(DropTargetEvent e){

}
public void dragEnter(DropTargetDragEvent e){

}
}

public BufferedImage resizedImage(File f){
BufferedImage bi=new BufferedImage

(100,100,BufferedImage.TYPE_INT_ARGB);

try{
BufferedImage or=ImageIO.read(f);
Graphics2D g2d=bi.createGraphics();
g2d.drawImage(or,0,0,100,100,null);
}catch(IOException e){e.printStackTrace();}
return bi;
}

}


public class DragAndDrop {
public static void main(String[] args){
new JLabelShow("Drag and Drop");
}
}

3 comments:

  1. That great!!! It help me a lot.Thank teacher.

    ReplyDelete
  2. Yes, back in 2013 that could be a trouble. You have pretty great solutions posted here though. Thank you, you are doing a nice work.

    ReplyDelete
  3. nice article in your blog.thank you for sharing useful info.
    visit
    web programming tutorial
    welookups

    ReplyDelete