B4J Question Having a problem with jDragAndDrop2

DaleA

Member
I am having a problem getting an image to drop onto an ImageView using the jDragAndDrop2 library. In the Designer, I added an ImageView, named ImgTarget, onto a Pane and made sure the ImageView is in front.. Below is the entire code I'm testing with.

B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    
    Private DragDrop2 As DragAndDrop
    Private ImageIndex As Int

    Private ImgTarget As ImageView
End Sub

Public Sub Initialize
    
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    
    DragDrop2.Initialize(Me)
    DragDrop2.MakeDragTarget(ImgTarget, "ImgTarget")
    
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
    xui.MsgboxAsync("Hello world!", "B4X")
End Sub

Sub ImgTarget_DragEntered(e As DragEvent)
    Log("ImgTDragEntered")
        
End Sub

Most of it was copied directly from the example supplied with the library.
The problem I'm seeing is that when I drag a .jpg image onto the ImageView it doesn't recognize that a drag gesture is being done and the log entry in ImgTarget_DragEntered doesn't get logged. The example program works fine so there must be something I'm doing wrong or something I missed. But what?
 

DaleA

Member
Uh, nope. I added the DragOver code below (again copied directly from the DragAndDrop example (although I removed the URL portion) and still can't drag an image onto the ImageView.

B4X:
Sub ImgTarget_DragOver(e As DragEvent)
        
    If e.GetDragboard.HasImage Then
        e.AcceptTransferModes(TransferMode.COPY)
        Return
    End If
    If e.GetDragboard.HasFiles Then
        Dim Found As Boolean = False
        For Each F As String In e.GetDragboard.GetFiles
            If F.EndsWith(".jpg") Then Found = True
            If F.EndsWith(".png") Then Found = True
        Next
        If Found Then
            e.AcceptTransferModes(TransferMode.COPY)
            Return
        End If
    End If
        
End Sub

I don't see how adding the DragOver sub would matter if the DragEntered sub isn't being called. A break point on the first statement in the ImgTarget_DragEntered sub never gets hit which does indicate that the DragEntered sub isn't being called.

As a test, I added a TextArea and set it up to accept a DragAndDrop...
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    
    Private DragDrop1 As DragAndDrop
    Private DragDrop2 As DragAndDrop
    Private ImageIndex As Int

    Private ImgTarget As ImageView
    Private TxtTarget As TextArea
End Sub

Public Sub Initialize
    
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    
    DragDrop1.Initialize(Me)
    DragDrop1.MakeDragTarget(TxtTarget, "TxtTarget")
    DragDrop2.Initialize(Me)
    DragDrop2.MakeDragTarget(ImgTarget, "ImgTarget")
    
End Sub

Private Sub Button1_Click
    xui.MsgboxAsync("Hello world!", "B4X")
End Sub

Sub ImgTarget_DragEntered(e As DragEvent)
    Log("ImgTDragEntered")
        
End Sub

Sub TxtTarget_DragEntered(e As DragEvent)
    Log("TxtTDragEntered")
        
End Sub

When I drag something onto the TextArea, the log shows the TxtTDragEntered message as it should. Yet the same code doesn't seem to work for the ImageView, the ImgTDragEntered message never shows in the log.

Next, if I replace the ImageView with a Canvas, the code works correctly and I get the ImgTDragEntered message when I drag something onto the canvas. So why would it work with a Canvas but not with an ImageView?
 
Upvote 0

DaleA

Member
Further investigation leads me to discovering that the issue seems to be that the ImageView object isn't getting the DragEntered event. Instead, the event seems to be intercepted by the Pane that the ImageView has as it's parent. I found this by adding drop code for the Pane itself and it was executed. So, the questions are 1) why is the same setup in the Example program supplied with the library working and mine isn't, 2) how do I get the Pane to pass the event to the ImageView object, and 3) what would cause the same structure in the Designer to work in one app and not in another?

I need a drink!
 
Upvote 0

DaleA

Member
Put the ImageView in a panel and use this panel to intercept the events.
That is exactly what is happening in my test application. What I don't understand is why it works differently in the example app you posted in posting #3 of the thread jDragAndDrop2 - Drag and Drop. In your example, the ImageView is in a Pane but still gets the events whereas in my app the Pane gets them. Since I'm still in the learning phase, I assume it's something I did (or didn't do) wrong but I can't figure out what.
 
Upvote 0
Top