B4J Library jDragAndDrop2 - Drag and Drop

This library is an update to Andrews jDragandDrop to take advantage of the DragBorad.DragView available in JavaFX8. This allows displaying a graphic next to or behind the mouse cursor while dragging items.

It's not quite a drop in replacement, but I tried to make it as close as possible.

The differences are:
  1. The DragAndDrop class needs to be initialized with the Callback module.
  2. The Transfer Mode does not support strings, it needs to be a TransferMode array, which are available as variables from the TransferMode static class.
  3. The e.AcceptTransferMode call has changed to e.AcceptTransferModes.
  4. There are two additional SetDragModeAndData methods to cater for setting the DragView.
  5. I have exposed most of the Dragboard methods which make it easier to select the data you want to accept, and get the results from the dragboard, and most of the DragEvent methods. Existing code should work as is.
  6. If you use sender to get the current EventSource (as opposed to the GestureSource) you will need to change it from sender to e.GetEventSource
The demo shows drag and drop from within the app, and from outside it for text and images.

Issues:
The only issue I found, and it may be a 'feature'. Is that if an image is being dragged, a copy of that image is used in place of whatever is set in the dragview. I would have preferred to be able to set a thumbnail, but it doesn't appear to work like that.​

It's written as 4 code modules, you can compile it to a library if you prefer.
Please let me know if you find any problems with it.

For a b4xlib, see Erel's implementation in post #3

Enjoy.

Update v1.2 - Changed global variable Initialized to IInitialized to avoid issues with B4x V10.2 Beta. There is now only a B4xlib version.
 

Attachments

  • DragAndDrop2-b4xlib.b4xlib
    6.9 KB · Views: 42
Last edited:

alirezahassan

Active Member
Licensed User
thank you dear @stevel05,
I solved my problem. The following code was originally my code.
old code:
Sub DropTarget_Text_DragDropped(e As DragEvent)
    Try
        Dim pathsss As String = File.GetFileParent(e.GetDragboard.GetFiles.Get(0))
        Dim namesss As String = File.GetName(e.GetDragboard.GetFiles.Get(0))
        TXT_Text.Visible = False
        IMG_Text.Visible = True
        Wait For (Codes.Compress(pathsss,namesss)) Complete (Completed As Image)
        IMG_Text.SetImage(Completed)
        Loadings.Loading_Show(MainForm)
-->  uploudFile(File.GetFileParent(e.GetDragboard.GetFiles.Get(0)),File.GetName(e.GetDragboard.GetFiles.Get(0)),"Text")
        e.SetDropCompleted(True)
    Catch
        Log(LastException)
    End Try
End Sub
I changed the above code
New code:
Sub DropTarget_Text_DragDropped(e As DragEvent)
    Try
        Dim pathsss As String = File.GetFileParent(e.GetDragboard.GetFiles.Get(0))
        Dim namesss As String = File.GetName(e.GetDragboard.GetFiles.Get(0))
        TXT_Text.Visible = False
        IMG_Text.Visible = True
        Wait For (Codes.Compress(pathsss,namesss)) Complete (Completed As Image)
        IMG_Text.SetImage(Completed)
        Loadings.Loading_Show(MainForm)
 --> uploudFile(pathsss,namesss,"Text")
        e.SetDropCompleted(True)
    Catch
        Log(LastException)
    End Try
End Sub
I realized that this error occurred after the (Wait For) code. After the (Wait For) code, the variable e was cleared! It is as if the inputs are empty after the (Wait For) code.
I defined two variables. And after the (Wait For) code, its value was not cleared. I think this is a bug @Erel.
Thanks for following up Mr. @stevel05
 

stevel05

Expert
Licensed User
Longtime User
Yes, you accept the dragged item in the _DragOver sub for each target, so you can use various methods to identify if you want to accept the drop for each target.
i.e. FileType, dragboard content type, or dataid (set in _Source_DragDetected sub) etc.

You just need to create a different DragAndDrop Class instance and call MakeDragTarget on it for each target and create the related subs. You don't need to call MakeDragSource for each class instance.
 
Last edited:

stevel05

Expert
Licensed User
Longtime User
Yes, I noticed your post and am in the process of changing it. I will only be publishing a b4xlib for this and not a compiled library. Fortunately it is an internal variable. I'll post it here as soon as I've updated it.
 

stevel05

Expert
Licensed User
Longtime User
OK, I've replaced the original zip with a B4xlib, I am in the process of setting up a new PC, so I hope I've picked the right project ::)

Replaced an internal global Initialized with IInitialized. I apologise If you were using the original compiled library, hopefully there aren't any breaking changes. I have tested it on a couple of my own projects. Let me know if you find any problems.
 

stevel05

Expert
Licensed User
Longtime User
I don't like either of them, neither blnInitialized nor IInitialized
No, but it's late and my brain couldn't think of anything else. Might have taken another week to come up with something.

There are a few version on different posts around the forum, I'll have to see if I can find them and remove the old ones.
 

stevel05

Expert
Licensed User
Longtime User
I like having B4xlib in the name so that I can identify them in the long list of libraries I have, and adding 1 character to a variable name is not really a whole point version change.
 
Top