[英]JTable Drag and Drop using TransferHandler
Currently I am having two tables say Table-A, Table-B
. 目前,我有两个表,分别是
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. 我的任务是将表A中的行拖到表B中或将表B中的行拖到自身上。 Using
TransferHandler
I have achieved this task. 使用
TransferHandler
我已经完成了这项任务。 But my problem is, I am not able to recognize from which table row was dragged to Table-B
.. ie, either from Table-A to Table-B or from Table-B on to itself. 但是我的问题是,我无法识别从哪个表行拖动到
Table-B
..即从表A拖动到Table-B
还是从表Table-B
拖动到自身。 In exportData
method of TransferHandler
I am adding some additional data to one of the column. 在
TransferHandler
exportData
方法中,我向该列之一添加了一些其他数据。 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. 基于此,当我通过
importData
方法导入数据时,我能够使用添加数据的特定列来弄清楚它来自哪个表。 I know this is not at all recommended .. so I need to know if there is a good way to approach this issue? 我知道完全不建议这样做。所以我需要知道是否有解决此问题的好方法?
You can create your own implementation of Transferable
that will have a reference to the source component. 您可以创建自己的
Transferable
实现,该实现将引用源组件。 Then in TransferHandler.importData()
you can compare it with TransferSupport.getComponent()
which is a destination component. 然后,可以在
TransferHandler.importData()
中将其与作为目标组件的TransferSupport.getComponent()
进行比较。
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. 这是一个非常基本的
Transferable
实现。
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);
}
}
Then, in TransferHandler
: 然后,在
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.