Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 

File Dropper Java code example - Click here to copy ->>>

   Can't find what you're looking for? Try our search:

Really working examples categorized by API, package, class. You can compile and run our examples right away! Not from source code for Java projects - only working examples! Copy, compile and run!

Code:

 
 import java.awt.*;
import javax.swing.*;
import java.awt.datatransfer.*;
import javax.swing.filechooser.*;
import java.io.*;
import java.awt.dnd.*;
import java.util.List;
import java.util.ArrayList;
import java.awt.image.*;

public class FileDropper {

    /*

        //create frame and label and text editor and quit corner
        //create drag support to filesystem to provide a file
        //set proper icon and add file image (resized properly)
        ??fix image translucency problem
        //adjust cursor when over valid or invalid drop point.
    */

    public static void main(String[] args) throws IOException {
        JFrame frame = new JFrame("Drag and Drop File Hack");
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);

        FileSystemView fsv = FileSystemView.getFileSystemView();
        Icon icon = fsv.getSystemIcon(File.createTempFile("myfile.",".txt"));
      System.out.println("icon = " + icon);
        //ImageIcon iicn = (ImageIcon)icon;

        frame.getContentPane().setLayout(new BorderLayout());
        JTextArea text = new JTextArea();

        JLabel label = new JLabel("myfile.txt",icon,SwingConstants.CENTER);
        DragSource ds = DragSource.getDefaultDragSource();
        DragGestureRecognizer dgr = ds.createDefaultDragGestureRecognizer(
            label,
            DnDConstants.ACTION_MOVE,
            new FileDragGestureListener(text));

        frame.getContentPane().add("North",label);
        frame.getContentPane().add("Center",text);

        frame.pack();
        frame.setSize(400,300);
        frame.setVisible(true);
    }
}


class FileDragGestureListener extends DragSourceAdapter implements DragGestureListener {
    JTextArea text;
    public FileDragGestureListener(JTextArea text) {
        this.text = text;
    }
    Cursor cursor;
    public void dragGestureRecognized(DragGestureEvent evt) {
        try {

            // generate the temp file
            File temp_dir = File.createTempFile("tempdir",".dir",null);
            File temp = new File(temp_dir.getParent(),"myfile.txt");
            FileOutputStream out = new FileOutputStream(temp);
            out.write(text.getText().getBytes());
            out.close();

            // get the right icon
            FileSystemView fsv = FileSystemView.getFileSystemView();
            Icon icn = fsv.getSystemIcon(temp);

            // we could cast to an image icon, but it might not be one.
            // painting to a buffer first also solves the problem of passing in the
            // the right sized buffer because the cursor might scale it
            // convert to the right sized image
            Toolkit tk = Toolkit.getDefaultToolkit();
            Dimension dim = tk.getBestCursorSize(icn.getIconWidth(),icn.getIconHeight());
            BufferedImage buff = new BufferedImage(dim.width,dim.height,BufferedImage.TYPE_INT_ARGB);
            icn.paintIcon(text,buff.getGraphics(),0,0);

            // set up drag image
            if(DragSource.isDragImageSupported()) {
                evt.startDrag(DragSource.DefaultCopyDrop, buff, new Point(0,0),
                        new TextFileTransferable(temp),
                        this);
            } else {
                cursor = tk.createCustomCursor(buff,new Point(0,0),"billybob");
                evt.startDrag(cursor, null, new Point(0,0),
                        new TextFileTransferable(temp),
                        this);
            }

        } catch (IOException ex) {
            System.out.println("exception: " + ex.getMessage());
        }
    }


    public void dragEnter(DragSourceDragEvent evt) {
        DragSourceContext ctx = evt.getDragSourceContext();
        //System.out.println("doing a move: " + evt.getDropAction());
        ctx.setCursor(cursor);
    }

    public void dragExit(DragSourceEvent evt) {
        //        p("exit");
        DragSourceContext ctx = evt.getDragSourceContext();
        ctx.setCursor(DragSource.DefaultCopyNoDrop);
    }

    /*
    public void dragOver(DragSourceDragEvent evt) {

    }

    public void dragDropEnd(DragSourceDropEvent evt) {
        //p("drag drop end");
    }
    */

    public static void p(String str) {
        System.out.println(str);
    }
}

// create a transferable for the right data flavor
class TextFileTransferable implements Transferable {
    File temp;

    public TextFileTransferable(File temp) throws IOException {
        this.temp = temp;
    }

    public Object getTransferData(DataFlavor flavor) {
        p("get trans data called");
        List list = new ArrayList();
        list.add(temp);
        return list;
    }

    public DataFlavor[] getTransferDataFlavors() {
        DataFlavor[] df = new DataFlavor[1];
        df[0] = DataFlavor.javaFileListFlavor;
        return df;
    }

    public boolean isDataFlavorSupported(DataFlavor flavor) {
        if(flavor == DataFlavor.javaFileListFlavor) {
            return true;
        }
        return false;
    }

    public static void p(String str) {
        System.out.println(str);
    }
}




/*   Swing Hacks
 *   Tips and Tools for Killer GUIs
 * By Joshua Marinacci, Chris Adamson
 *   First Edition June 2005
 *   Series: Hacks
 *   ISBN: 0-596-00907-0
 *   http://www.oreilly.com/catalog/swinghks/
 */

 
 



References.

The list of classes which were used on this page you can find below. The links to Java API contain official SUN documentation about all used classes.




[ Go Back ]



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy