アプリケーションが特定の拡張子 (例: wrl) のファイルのみを受け入れるように DnD を実装したいと考えています。ファイルが受け入れられる場合はカーソルをドラッグカーソルに変更し、間違った拡張子のファイルがドラッグアンドドロップされた場合は通常のカーソルに戻したいです。
http://docs.oracle.com/javase/tutorial/uiswing/dnd/toplevel.htmlの次のチュートリアル/例に従っています
私が行う変更は canImport 関数にあります
public boolean canImport(TransferHandler.TransferSupport support)
{
if (!support.isDataFlavorSupported(DataFlavor.javaFileListFlavor))
{
return false;
}
Transferable t = support.getTransferable();
try
{
java.util.List<File> l = (java.util.List<File>)t.getTransferData(DataFlavor.javaFileListFlavor);
if (l.size() != 1)
{
return false;
}
File f = l.get(0);
String extension = f.getName().substring(f.getName().lastIndexOf("."));
return extension.equalsIgnoreCase(".wrl");
}
catch (UnsupportedFlavorException e)
{
return false;
}
catch (IOException e)
{
return false;
}
}
これは、別のファイルにドラッグすると正しく動作します。ただし、ファイルをドロップすると、次の例外が発生します
Exception in thread "AWT-EventQueue-0" java.awt.dnd.InvalidDnDOperationException: No drop current
at sun.awt.dnd.SunDropTargetContextPeer.getTransferData(SunDropTargetContextPeer.java:227)
at sun.awt.datatransfer.TransferableProxy.getTransferData(TransferableProxy.java:56)
at java.awt.dnd.DropTargetContext$TransferableProxy.getTransferData(DropTargetContext.java:359)
at appl.TopLevelTransferHandlerDemo$1.canImport(TopLevelTransferHandlerDemo.java:207)
at javax.swing.TransferHandler$DropHandler.drop(TransferHandler.java:1454)
at java.awt.dnd.DropTarget.drop(DropTarget.java:434)
at javax.swing.TransferHandler$SwingDropTarget.drop(TransferHandler.java:1193)
at sun.awt.dnd.SunDropTargetContextPeer.processDropMessage(SunDropTargetContextPeer.java:519)
at sun.awt.dnd.SunDropTargetContextPeer$EventDispatcher.dispatchDropEvent(SunDropTargetContextPeer.java:832)
at sun.awt.dnd.SunDropTargetContextPeer$EventDispatcher.dispatchEvent(SunDropTargetContextPeer.java:756)
at sun.awt.dnd.SunDropTargetEvent.dispatch(SunDropTargetEvent.java:30)
at java.awt.Component.dispatchEventImpl(Component.java:4487)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processDropTargetEvent(Container.java:4312)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4163)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
どんな助けでも大歓迎です