过去一周我试图解决这个问题,但不知何故我似乎无法找到解决方案。关于这个主题的信息不多,因此很难找到示例或代码来查看。
我这里有一个 JList,它使用创建自定义 Transferable 的自定义 TransferHandler,供参考,这里是所涉及类的代码:
可转让:
package org.dinhware.swing.special;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
/**
* Created by: Niklas
* Date: 20.10.2017
* Alias: Dinh
* Time: 20:03
*/
public class GenericTransferable<T> implements Transferable {
static DataFlavor FLAVOR;
private T object;
GenericTransferable(T object) {
GenericTransferable.FLAVOR = new DataFlavor(object.getClass(), object.getClass().getCanonicalName());
this.object = object;
}
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{FLAVOR};
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return flavor.equals(FLAVOR);
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
return object;
}
}
传输处理程序:
package org.dinhware.swing.special;
import javax.swing.*;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
/**
* Created by: Niklas
* Date: 19.10.2017
* Alias: Dinh
* Time: 18:54
*/
@SuppressWarnings("unchecked")
public class HListItemTransferHandler<T> extends TransferHandler {
@Override
protected Transferable createTransferable(JComponent component) {
JList<T> list = (JList<T>) component;
index = list.getSelectedIndex();
T transferredObject = list.getSelectedValue();
return new GenericTransferable<>(transferredObject);
}
@Override
public boolean canImport(TransferSupport info) {
return info.isDataFlavorSupported(GenericTransferable.FLAVOR);
}
@Override
public int getSourceActions(JComponent c) {
return MOVE;
}
@Override
public boolean importData(TransferSupport info) {
if (!canImport(info)) {
return false;
}
JList<Object> target = (JList<Object>) info.getComponent();
JList.DropLocation dl = (JList.DropLocation) info.getDropLocation();
DefaultListModel<Object> listModel = (DefaultListModel<Object>) target.getModel();
int index = dl.getIndex();
int max = listModel.getSize();
if (index < 0 || index > max)
index = max;
addIndex = index;
try {
Object object = info.getTransferable().getTransferData(GenericTransferable.FLAVOR);
listModel.add(index, object);
target.addSelectionInterval(index, index);
return moveAllowed = true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
return false;
}
@Override
protected void exportDone(JComponent c, Transferable data, int action) {
if (moveAllowed)
cleanup(c, action == MOVE, false);
}
private void cleanup(JComponent component, boolean remove, boolean bin) {
if (remove && index != -1) {
JList<T> source = (JList<T>) component;
DefaultListModel<T> model = (DefaultListModel<T>) source.getModel();
int removeAt = index > addIndex ? index + 1 : index;
model.remove(bin ? removeAt - 1 : removeAt);
}
index = -1;
addIndex = -1;
moveAllowed = false;
}
private int index = -1;
private int addIndex = -1;
private boolean moveAllowed = false;
}
HBin
package org.dinhware.swing.child;
import org.dinhware.swing.special.HListItemTransferHandler;
import javax.swing.*;
/**
* Created by: Niklas
* Date: 20.10.2017
* Alias: Dinh
* Time: 19:57
*/
public class HBin<T> extends HImageLabel {
public HBin(String text, Icon image, int distance) {
super(text, image, distance);
setTransferHandler(new HListItemTransferHandler<T>());
}
}
以及它应该如何工作的可视化,可悲的是,即使没有将容器拖到 Hbin 容器上,它也总是会消失。我一直以为它一直在工作,直到我不小心把它移到了我的框架之外,它仍然消失了。上面的代码只允许在预期的列表内拖放。
我的问题是如何添加功能以正确使容器仅在拖到 Hbin 容器上时才消失
我使用的代码的第一部分是这个
@Override
protected void exportDone(JComponent c, Transferable data, int action) {
if (moveAllowed) cleanup(c, action == MOVE, false);
else try {
if (data.getTransferData(GenericTransferable.FLAVOR) instanceof RewardItem) {
cleanup(c, true, true);
}
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
我背后的逻辑是 List 和 Hbin 共享相同的类型(RewardItem),我可以比较,后来我意识到(在制作该方法的更通用版本之后)数据将始终是 RewardItem 类型并且总是会导致在清理电话中。这导致了我目前仍然面临的错误。
我今天早些时候采取的方法真的让我质疑自己的想法,也让我写了这篇文章。我在名为 bin 的 TransferHandler 中添加了一个布尔值,默认情况下为 false。在 canImport 签入 importData 之后,我添加bin = info.getComponent() instanceof HBin
了我认为应该可以工作的内容。但这个领域总是假的。我继续为它添加了一个日志
System.out.println("IMPORT");
if (info.getComponent() instanceof HBin) {
System.out.println("bin");
return bin = true;
}
它最终打印了 IMPORT,然后是 bin。在调用 importData exportData 之后,我在其中记录了 bin 的值,无论出于何种原因,它现在再次为 false。与此同时,moveAllowed 字段似乎发生了变化。
这是我完全修改过的 TransferHandler
package org.dinhware.swing.special;
import org.dinhware.swing.child.HBin;
import javax.swing.*;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
/**
* Created by: Niklas Date: 19.10.2017 Alias: Dinh Time: 18:54
*/
@SuppressWarnings("unchecked")
public class HListItemTransferHandler<T> extends TransferHandler {
@Override
protected Transferable createTransferable(JComponent component) {
System.out.println("CREATE");
JList<T> list = (JList<T>) component;
index = list.getSelectedIndex();
T transferredObject = list.getSelectedValue();
return new GenericTransferable<>(transferredObject);
}
@Override
public boolean canImport(TransferSupport info) {
return info.isDataFlavorSupported(GenericTransferable.FLAVOR);
}
@Override
public int getSourceActions(JComponent c) {
System.out.println("ACTION");
return MOVE;
}
@Override
public boolean importData(TransferSupport info) {
System.out.println("IMPORT");
if (!canImport(info)) {
return false;
}
if (info.getComponent() instanceof HBin) {
System.out.println("bin");
return bin = true;
}
JList<Object> target = (JList<Object>) info.getComponent();
JList.DropLocation dl = (JList.DropLocation) info.getDropLocation();
DefaultListModel<Object> listModel = (DefaultListModel<Object>) target.getModel();
int index = dl.getIndex();
int max = listModel.getSize();
if (index < 0 || index > max)
index = max;
addIndex = index;
try {
Object object = info.getTransferable().getTransferData(GenericTransferable.FLAVOR);
listModel.add(index, object);
target.addSelectionInterval(index, index);
return moveAllowed = true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
return false;
}
@Override
protected void exportDone(JComponent c, Transferable data, int action) {
System.out.println("EXPORT " + moveAllowed + "/" + bin);
if (moveAllowed)
cleanup(c, action == MOVE, false);
else
cleanup(c, true, true);
}
private void cleanup(JComponent component, boolean remove, boolean bin) {
System.out.println("CLEAN");
if (remove && index != -1) {
JList<T> source = (JList<T>) component;
DefaultListModel<T> model = (DefaultListModel<T>) source.getModel();
int removeAt = index > addIndex ? index + 1 : index;
model.remove(bin ? removeAt - 1 : removeAt);
}
index = -1;
addIndex = -1;
moveAllowed = false;
}
private int index = -1;
private int addIndex = -1;
private boolean moveAllowed = false, bin = false;
}
在列表内移动时一切正常(打印)
ACTION
CREATE
IMPORT
EXPORT true/false
CLEAN
但是当掉到 Hbin 容器上时,我无法解释发生了什么(打印)
ACTION
CREATE
IMPORT
bin
EXPORT false/false
我很确定它应该是假/真
现在我被卡住了,无法使容器仅在放到 Hbin 上时消失,同时也对字段值在明确记录它已设置为 true 时没有改变感到困惑。
请帮忙...