java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4091
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Drag and drop of a group of files into a tree E-mail
User Rating: / 13
PoorBest 

This Java Swing tip illustrates a method of implementing functionality of drag & drop of a group of files into a tree. The tip uses getdatatransfer function that returns an object that represents the data to be transferred. The class of the object returned is defined by the representation class of the data flavor.

import javax.swing.*;
import javax.swing.tree.*;
import java.awt.dnd.*;
import java.awt.datatransfer.*;
import java.util.List;
import java.util.Iterator;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Insets;
import java.io.File;
import java.io.IOException;

public class FSTree extends JTree {

  public FSTree() { super(); init()}
  public FSTree(TreeModel newModel) { super(newModel); init()}
  public FSTree(TreeNode root) { super(root); init()}
  public FSTree(TreeNode root, boolean asks) { super(root, asks); init()}

  private void init() {
    // We don't want to export anything from this tree, only import
    setDragEnabled(false);
    setTransferHandler(new FSTransfer());
  }

  public class FSTransfer extends TransferHandler {
    public boolean importData(JComponent comp, Transferable t) {
      // Make sure we have the right starting points
      if (!(comp instanceof FSTree)) {
        return false;
      }
      if (!t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
        return false;
      }

      // Grab the tree, its model and the root node
      FSTree tree = (FSTree)comp;
      DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
      DefaultMutableTreeNode root = (DefaultMutableTreeNode)model.getRoot();
      try {
        List data = (List)t.getTransferData(DataFlavor.javaFileListFlavor);
        Iterator i = data.iterator();
        while (i.hasNext()) {
          File f = (File)i.next();
          root.add(new DefaultMutableTreeNode(f.getName()));
        }
        model.reload();
        return true;
      }
      catch (UnsupportedFlavorException ufe) {
        System.err.println("Ack! we should not be here.\nBad Flavor.");
      }
      catch (IOException ioe) {
        System.out.println("Something failed during import:\n" + ioe);
      }
      return false;
    }
    
    // We only support file lists on FSTrees...
    public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) {
      if (comp instanceof FSTree) {
        for (int i = 0; i < transferFlavors.length; i++) {
          if (!transferFlavors[i].equals(DataFlavor.javaFileListFlavor)) {
            return false;
          }
        }
        return true;
      }
      return false;
    }
  }
}

 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.