简体   繁体   中英

Dragging nodes from a Jtree to the operating system in swing

I have read and found many ways to drag JComponent s into other JComponent s or drag files onto my JComponent s from the OS to the java application. However I would like to drag a node of my jtree outside of my application into a directory. My nodes represent files so i'd like to save them in the directory they have been dragged.

Does anyone know of a way to do so using the TransferHandler ?

The method public void exportDone(JComponent c, Transferable t, int action) does not allow me to find out into what path the file (node) was dragged naturally...

The method public void exportDone(JComponent c, Transferable t, int action) does not allow me to find out into what path the file (node) was dragged naturally...

import java.awt.*;
import java.awt.datatransfer.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.tree.*;

public class TreeDragAndDropTest {
  public JComponent makeUI() {
    JTree tree = new JTree();
    tree.getSelectionModel().setSelectionMode(
        TreeSelectionModel.SINGLE_TREE_SELECTION);
    tree.setDragEnabled(true);
    tree.setTransferHandler(new TransferHandler() {
      private File f;
      @Override public int getSourceActions(JComponent c) {
        return COPY;
      }
      @Override protected Transferable createTransferable(JComponent c) {
        TreePath p = ((JTree) c).getSelectionPath();
        DefaultMutableTreeNode n = (DefaultMutableTreeNode) p.getLastPathComponent();
        try {
          f = File.createTempFile(Objects.toString(n.getUserObject()) + "_", ".tmp");
        } catch (IOException ex) {
          ex.printStackTrace();
        }
        if (Objects.nonNull(f)) {
          return new Transferable() {
            @Override public Object getTransferData(DataFlavor flavor) {
              return Arrays.asList(f);
            }
            @Override public DataFlavor[] getTransferDataFlavors() {
              return new DataFlavor[] { DataFlavor.javaFileListFlavor };
            }
            @Override public boolean isDataFlavorSupported(DataFlavor flavor) {
              return flavor.equals(DataFlavor.javaFileListFlavor);
            }
          };
        } else {
          return null;
        }
      }
      @Override protected void exportDone(JComponent c, Transferable d, int a) {
        if (Objects.nonNull(f)) {
          f.delete();
        }
      }
    });
    JPanel p = new JPanel(new BorderLayout());
    p.add(new JScrollPane(tree));
    return p;
  }
  public static void main(String... args) {
    EventQueue.invokeLater(() -> {
      JFrame f = new JFrame();
      f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      f.getContentPane().add(new TreeDragAndDropTest().makeUI());
      f.setSize(320, 240);
      f.setLocationRelativeTo(null);
      f.setVisible(true);
    });
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM