简体   繁体   English

将节点从Jtree拖到Swing中

[英]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. 我已经阅读并找到了许多方法来将JComponent拖到其他JComponent或将文件从操作系统拖到我的JComponent到Java应用程序。 However I would like to drag a node of my jtree outside of my application into a directory. 但是,我想将jtree的节点拖到应用程序之外到目录中。 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 ? 有谁知道使用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... 方法public void exportDone(JComponent c, Transferable t, int action)不允许我自然地将文件(节点)拖到哪个路径中...

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... 方法public void exportDone(JComponent c,Transferable t,int action)不允许我自然地将文件(节点)拖到哪个路径中...

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);
    });
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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