B4J Code Snippet Form Change Cursor Image

To share a snippet which changes the form cursor to an image which is located in the files (dirasset) folder.

Acknowledgement: just found this thread, which relates as showing an animated cursor - was not aware prior posting this snippet.

B4X:
' Example: SetFormCursor(MainForm, "arrowup.png")
Sub SetFormCursor(frm As Form, imagefile As String)
    Dim img As Image
    img.Initialize(File.DirAssets, imagefile)
    Dim joImageCursor As JavaObject
    joImageCursor.InitializeNewInstance("javafx.scene.ImageCursor", Array(img))
    Dim joCursor As JavaObject = frm.RootPane
    joCursor.RunMethod("setCursor", Array(joImageCursor))
End Sub

The image arrowup.png mentioned as example:
upload_2016-11-7_18-4-49.png
 
Last edited:

BeneBarros

Active Member
Licensed User
Longtime User
Excellent!! I found what I was looking for.
I just made small changes to determine hotspotX and hotspotY

for

Sub SetFormCursor (frm As Form , imagefile As String, hsX As Double, hsY As Double)

and

JoImageCursor.InitializeNewInstance ("javafx.scene.ImageCursor", Array (img, hsX, hsY))

Thank you!
 

ThRuST

Well-Known Member
Licensed User
Longtime User
Last edited:
Top