제 신청서에 JTree와 JList가 나란히 있습니다. JTree에서 JList로 노드를 드래그하면 List<File> copies;
에 추가 한 노드가 내 JList의 transferhandler에 boolean canImport()
메서드에 의해 거부됩니다 (Flavor not supported
).JList에 드롭 할 javafilelistflavor 객체는 어떻게 만듭니 까?
System.out.println(Arrays.toString(info.getTransferable().getTransferDataFlavors()));
반환 :
[javax.activation.ActivationDataFlavor[mimetype=application/x-java-jvm-local-objectref;representationclass=java.io.InputStream]]
은 어떻게 JTree가에서 드롭을 받아 내 JList를받을 수 있나요? 내 자신의 dataflavor를 만들려고했지만 실패했습니다. 붙여 넣은 코드에서 주석 처리되었습니다.
static class ListTransferHandler extends TransferHandler {
Logger lgs = null;
int action;
public ListTransferHandler(int action) {
this.action = action;
lgs = Logger.getLogger("ListTransferHandler Logger");
lgs.setLevel(ALL);
lgs.log(Level.INFO, "ListTransferHandler object created");
}
@Override
public boolean canImport(TransferHandler.TransferSupport info) {
// lgs.log(Level.INFO, "can import called");
if (!info.isDrop()) {
return false;
}
info.setShowDropLocation(true);
System.out.println(Arrays.toString(info.getTransferable().getTransferDataFlavors()));
if (!info.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
lgs.log(Level.INFO, "Flavor not supported");
return false;
} else {
lgs.log(Level.INFO, "Flavor supported");
}
boolean actionSupported = (action & info.getSourceDropActions()) == action;
if (!actionSupported) {
info.setDropAction(action);
return false;
}
return true;
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (!canImport(support)) {
return false;
}
JList.DropLocation dl = (JList.DropLocation) support.getDropLocation();
int index = dl.getIndex();
List<File> fileList;
try {
fileList = (List) support.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
} catch (UnsupportedFlavorException | java.io.IOException e) {
return false;
}
JList list = (JList) support.getComponent();
DefaultListModel model = (DefaultListModel) list.getModel();
//insertElementAt(data, index);
for (File file : fileList) {
model.addElement(file.getName());
}
return true;
}
}
static class SelectedFileTransferHandler extends TransferHandler {
//private DataFlavor localObjectFlavor = new DataFlavor(List.class, "Array of items");
Logger lgs = null;
public SelectedFileTransferHandler() {
lgs = Logger.getLogger("SelectedFileTransferHandler Logger");
lgs.setLevel(ALL);
// lgs.log(Level.INFO, "Tranfer object created");
}
@Override
public int getSourceActions(JComponent comp) {
// lgs.log(Level.INFO, "got source actions");
return COPY;
}
@Override
public Transferable createTransferable(JComponent comp) {
// lgs.log(Level.INFO, "creating tranferable");
JTree tree = (JTree) comp;
TreePath[] paths = tree.getSelectionPaths();
List<File> nodesAsFiles = null;
if (paths != null) {
nodesAsFiles = new ArrayList<File>();
for (TreePath path : paths) {
nodesAsFiles.add((File) path.getLastPathComponent());
}
//File node = (File) paths.getLastPathComponent();
// copies.add(node);
for (File file : nodesAsFiles) {
System.out.println(file.getPath());
}
}
List<File> defensiveCopyOfNodesAsFiles = null;
if (nodesAsFiles != null) {
defensiveCopyOfNodesAsFiles = new ArrayList(nodesAsFiles);
}
return new DataHandler(defensiveCopyOfNodesAsFiles, javaJVMLocalObjectMimeType);
//return new DataHandler(transferedObjects, localObjectFlavor.getMimeType());
}
}