使用TransferHandler进行JTable拖放 [英] JTable Drag and Drop using TransferHandler

查看:208
本文介绍了使用TransferHandler进行JTable拖放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有两张桌子说 Table-A,Table-B 。我的任务是将行从Table-A拖到Table-B或将Table-B中的行拖到自身上。使用 TransferHandler 我已经完成了这项任务。但我的问题是,我无法识别从哪个表行被拖到表-B ..即,从表-A到表-B或从表-B对自己。在 exportData 方法> TransferHandler 我在其中一个列中添加了一些额外的数据。基于此,当我通过 importData 方法导入数据时,我能够找出它使用哪个表来使用我添加数据的特定列。我知道这根本不推荐..所以我需要知道是否有一个很好的方法来解决这个问题?

Currently I am having two tables say Table-A, Table-B. My task is to drag rows from Table-A to Table-B or drag rows from Table-B on to itself. Using TransferHandler I have achieved this task. But my problem is, I am not able to recognize from which table row was dragged to Table-B .. i.e, either from Table-A to Table-B or from Table-B on to itself. In exportData method of TransferHandler I am adding some additional data to one of the column. Basing on this, when I am importing data through importData method I am able to figure out from which table it came using that particular column to which I added data. I know this is not at all recommended .. so I need to know if there is a good way to approach this issue?

推荐答案

您可以创建自己的 Transferable 实现,它将引用源组件。然后在 TransferHandler.importData()中,您可以将它与 TransferSupport.getComponent()进行比较,它是目标组件。

You can create your own implementation of Transferable that will have a reference to the source component. Then in TransferHandler.importData() you can compare it with TransferSupport.getComponent() which is a destination component.

例如,这是一个将被转移的字符串的包装:

For example, here is a wrapper for a string that will be transferred:

public class DataWrapper {
    String data;
    Object source;

    public DataWrapper(String data, Object source) {
        super();
        this.source = source;
        this.data = data;
    }

    public String getData() {
        return data;
    }
    public Object getSource() {
        return source;
    }
}

这是一个非常基本的可转让的实施。

Here is a very basic Transferable implementation.

public class DataWrapperTransferable implements Transferable {
    public static DataFlavor FLAVOR = new DataFlavor(DataWrapper.class,
            "DataWrapper");

    private DataFlavor flavors[];
    private DataWrapper dataWrapper;

    public DataWrapperTransferable(String data, Object source) {
        dataWrapper = new DataWrapper(data, source);
        flavors = new DataFlavor[] { FLAVOR };
    }

    @Override
    public Object getTransferData(DataFlavor flavor)
            throws UnsupportedFlavorException, IOException {
        if (flavor.equals(FLAVOR)) {
            return dataWrapper;
        } else {
            throw new UnsupportedFlavorException(flavor);
        }
    }

    @Override
    public DataFlavor[] getTransferDataFlavors() {
        return flavors;
    }

    @Override
    public boolean isDataFlavorSupported(DataFlavor flavor) {
        return flavor.equals(FLAVOR) || flavor.equals(DataFlavor.stringFlavor);
    }
}

然后,在 TransferHandler

public boolean importData(TransferSupport support) {
    DataWrapper dataWrapper = (DataWrapper) support
            .getTransferable().getTransferData(
                    DataWrapperTransferable.FLAVOR);

    if (dataWrapper.getSource() == support.getComponent()) {
        //the originator and destination are the same 
    } else {
        //drop from another component
    }               

    //rest of the method
}

这篇关于使用TransferHandler进行JTable拖放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆