我正在尝试 Java SE 7 中 JList 的拖放功能。问题是,一旦我为 JList 设置了 DropTarget,我的 TransferHandler 实现中的 canImport() 方法就会停止调用。这是演示问题的简化代码:

主类:

import javax.swing.ListSelectionModel;

public class TestDragDrop extends javax.swing.JFrame {

    public TestDragDrop() {
        initComponents();
        lstTest.setDragEnabled(true);
        lstTest.setTransferHandler(new MyTransferHandler());
        // try to comment and uncomment the following line:
        lstTest.setDropTarget(new MyDropTarget());
        // - commented => canImport() is called during drag gesture
        // - uncommented => canImport() is not called during drag gesture
        lstTest.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        scroll1 = new javax.swing.JScrollPane();
        lstTest = new javax.swing.JList();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        lstTest.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        scroll1.setViewportView(lstTest);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(scroll1, javax.swing.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(scroll1, javax.swing.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>

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

    // Variables declaration - do not modify
    private javax.swing.JList lstTest;
    private javax.swing.JScrollPane scroll1;
    // End of variables declaration
}

我的传输处理程序实现:
import java.awt.datatransfer.Transferable;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.TransferHandler;

public class MyTransferHandler extends TransferHandler {

    @Override
    public int getSourceActions(JComponent component) {
        return COPY_OR_MOVE;
    }

    @Override
    protected Transferable createTransferable(JComponent component) {
        if(component instanceof JList) {
            return new MyTransferable(new String("TestObject"));
        }
        return null;
    }

    @Override
    public boolean canImport(TransferHandler.TransferSupport support) {
        System.out.println("canImport()");
        return true;
    }

    @Override
    public boolean importData(TransferHandler.TransferSupport support) {
        System.out.println("importData()");
        return true;
    }

}

我的可转让实现:
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

public class MyTransferable implements Transferable {

    private final Object object;

    public MyTransferable(Object object) {
        this.object = object;
    }

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

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

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

}

我的放置目标实现:
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDropEvent;

public class MyDropTarget extends DropTarget {

    @Override
    public synchronized void drop(DropTargetDropEvent evt) {
        evt.acceptDrop(evt.getDropAction());
        if(evt.isLocalTransfer()) {
            System.out.println("local transfer");
        } else {
            System.out.println("extern transfer");
        }
        evt.dropComplete(true);
    }

}

如果行 lstTest.setDropTarget(new MyDropTarget());主类中的 被关闭/注释, MyTransferHandler 中的 canImport() 方法在拖动手势期间按预期调用。但是一旦我取消注释这一行,在拖动过程中就不会调用 canImport() 方法...

有人知道为什么不再调用 canImport() 吗?

任何帮助高度赞赏!提前谢谢了。

最佳答案

setTransferHandler 如果不存在,则安装新的 DropTarget 并将设置它以使用指定的 TransferHandler 。之后用另一个 DropTarget 替换这个 DropTarget 将破坏这个设置。将自定义 TransferHandler 与以 Swing 方式工作的 ojit_code 结合起来相当棘手。以下将完成这项工作:

jComponent.setTransferHandler(new MyTransferHandler());
DropTarget original = jComponent.getDropTarget();// the Swing DropTarget
MyDropTarget myDropTarget = new MyDropTarget();
myDropTarget.addDropTargetListener(original);// delegate for original behavior
jComponent.setDropTarget(myDropTarget);

10-08 00:20