JTree: setTransferHandler(TransferHandler newHandler) (Drag and drop files from file explore) : JTree « javax.swing « Java by API






JTree: setTransferHandler(TransferHandler newHandler) (Drag and drop files from file explore)

  
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.TransferHandler;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;

public class FSTest extends JFrame {

  public FSTest() {
    super("FSTree Component Test");
    setSize(300, 300);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    FSTree fst = new FSTree(new DefaultMutableTreeNode("Starter"));
    getContentPane().add(new JScrollPane(fst));
    setVisible(true);
  }

  public static void main(String args[]) {
    new FSTest();
  }
}

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() {
    setDragEnabled(false);
    setTransferHandler(new FSTransfer());
  }

  public class FSTransfer extends TransferHandler {
    public boolean importData(JComponent comp, Transferable t) {
      if (!(comp instanceof FSTree)) {
        return false;
      }
      if (!t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
        return false;
      }

      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 examples in the same category

1.Tree.closedIcon
2.Tree.collapsedIcon
3.Tree.expandedIcon
4.Tree.leafIcon
5.Tree.openIcon
6.JTree.DynamicUtilTreeNode: createChildren(DefaultMutableTreeNode parent, Object children)
7.new JTree()
8.new JTree(TreeNode root)
9.new JTree(Vector vector)
10.JTree: addTreeSelectionListener(TreeSelectionListener tsl)
11.JTree: addTreeWillExpandListener(TreeWillExpandListener tel)
12.JTree: convertValueToText(Object v,boolean s,boolean e,boolean l,int r,boolean h)
13.JTree: getCellRenderer()
14.JTree: getLeadSelectionRow()
15.JTree: getMaxSelectionRow()
16.JTree: getMinSelectionRow()
17.JTree: getNextMatch(String prefix, int startingRow, Bias bias)
18.JTree: getPathForLocation(int x, int y)
19.JTree: getRowHeight()
20.JTree: getSelectionRows()
21.JTree: putClientProperty('JTree.lineStyle', 'None')
22.JTree: setAutoscrolls(boolean b)
23.JTree: setCellEditor(TreeCellEditor editor)
24.JTree: setExpandedState(TreePath path, boolean state)
25.JTree: setRowHeight(int rowHeight)