Swing drag / drop: accept / reject file list based on files or file types?

I use DropTarget and implement DropTargetListener to handle drop events, and it works great for accepting files (just see if there is a Transferable DataFlavor that returns true from isFlavorJavaFileListType() ).

Now I would like to ban certain types of garbage and give feedback accordingly:

  • Deny multiple files (only one accepted file)
  • deny a file whose file type is not in the accepted list

Can I do it? I think I remember working with XPCOM or COM, which was painful, in order to get enough information about the drop element, you had to accept it first, so there was a chicken / egg situation where you could only look at fragrance data, not the item itself, before you take a drop.

If I can’t abandon the discarded set of elements ahead of time, is there a corresponding user interface action (make a sound or something else) if the lost data is invalid?

+4
source share
1 answer
  • you can check the size of the list
  • you can use mime type library or check file extensions

If you want to play a sound, you can do the following:

 java.awt.Toolkit.getDefaultToolkit().beep(); 
+1
source

Source: https://habr.com/ru/post/1300340/


All Articles