gpt4 book ai didi

Java swing 如何启用 JScrollPane 的拖动/导出​​选项

转载 作者:行者123 更新时间:2023-11-30 05:57:56 24 4
gpt4 key购买 nike

我是 swing 新手,我正在尝试对 JScrollPane 实现拖动(导出)选项。我已经在我的 TransferHandler 类中实现了 drop(导入)选项:

final class FileDropHandler extends TransferHandler {
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
for (DataFlavor flavor : support.getDataFlavors()) {
if (flavor.isFlavorJavaFileListType()) {
return true;
}
}
return false;
}

@Override
@SuppressWarnings("unchecked")
public boolean importData(TransferHandler.TransferSupport support) {
if (!this.canImport(support))
return false;

List<File> files;
try {
files = (List<File>) support.getTransferable()
.getTransferData(DataFlavor.javaFileListFlavor);
} catch (UnsupportedFlavorException | IOException ex) {
// should never happen (or JDK is buggy)
return false;
}

for (File file: files) {
// do something...
}
return true;
}
}

将 Transfer Handler 设置为 JScrollPane 后,文件导入就像一个魅力:

scrollPane.setTransferHandler(new FileDropHandler());

但是如何将文件从 JScrollPane 导出到桌面或文件夹?我想导出文件。

最佳答案

看看下面的问题。我认为它不能完美地回答您的问题,但它应该为您导出文件提供一个良好的起点。 Swing application -> Drag & drop to the desktop / folder

以下是您可以运行的答案的修改示例:

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.TransferHandler;


public class DnDFrame extends javax.swing.JFrame implements DropTargetListener {

private DefaultListModel listModel = new DefaultListModel();
private DropTarget dropTarget;

public static void main(String[] args) {
DnDFrame dnd = new DnDFrame();
dnd.setVisible(true);
}

/** Creates new form DnDFrame */
public DnDFrame() {
initComponents();
dropTarget = new DropTarget(list, this);
list.setModel(listModel);
list.setDragEnabled(true);
list.setTransferHandler(new FileTransferHandler());
}

private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;

jLabel1 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
list = new javax.swing.JList();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new java.awt.GridBagLayout());

jLabel1.setText("Files:");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
getContentPane().add(jLabel1, gridBagConstraints);

jScrollPane1.setViewportView(list);

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
getContentPane().add(jScrollPane1, gridBagConstraints);

pack();
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JList list;

public void dragEnter(DropTargetDragEvent arg0) {
// nothing
}

public void dragOver(DropTargetDragEvent arg0) {
// nothing
}

public void dropActionChanged(DropTargetDragEvent arg0) {
// nothing
}

public void dragExit(DropTargetEvent arg0) {
// nothing
}

public void drop(DropTargetDropEvent evt) {
int action = evt.getDropAction();
evt.acceptDrop(action);
try {
Transferable data = evt.getTransferable();
if (data.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
List<File> files = (List<File>) data.getTransferData(
DataFlavor.javaFileListFlavor);
for (File file : files) {
listModel.addElement(file);
}
}
} catch (UnsupportedFlavorException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
evt.dropComplete(true);
}
}

private class FileTransferHandler extends TransferHandler {

@Override
protected Transferable createTransferable(JComponent c) {
JList list = (JList) c;
List<File> files = new ArrayList<File>();
for (Object obj: list.getSelectedValues()) {
files.add((File)obj);
}
return new FileTransferable(files);
}

@Override
public int getSourceActions(JComponent c) {
return MOVE;
}
}

private class FileTransferable implements Transferable {

private List<File> files;

public FileTransferable(List<File> files) {
this.files = files;
}

public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{DataFlavor.javaFileListFlavor};
}

public boolean isDataFlavorSupported(DataFlavor flavor) {
return flavor.equals(DataFlavor.javaFileListFlavor);
}

public Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException, IOException {
if (!isDataFlavorSupported(flavor)) {
throw new UnsupportedFlavorException(flavor);
}
return files;
}
}
}

关于Java swing 如何启用 JScrollPane 的拖动/导出​​选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52835630/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com