B4J Code Snippet Add a ToolTip to an ImageView

Hi,

this small code snippet adds a ToolTipText to an ImageView - as currently not exposed.

B4X:
'Add a tooltip to an ImageView
'JavaFX API Reference: http://download.java.net/jdk8/jfxdocs/javafx/scene/control/Tooltip.html
Sub ToolTipToImageView(iv As ImageView, msg As String)
    Dim joToolTip As JavaObject
    Dim joToolTip2 As JavaObject = joToolTip.InitializeNewInstance("javafx.scene.control.Tooltip", Array(msg))
    joToolTip.RunMethod("install", Array(iv, joToolTip2))
End Sub

Example:
B4X:
Private imgTMP36 As ImageView
ToolTipToImageView(imgTMP36, "The TMP36 temperature sensor")
 

Cableguy

Expert
Licensed User
Longtime User
Made a very small change to allow removing the tooltip .

B4X:
private Sub ToolTipToImageView(iv As ImageView, msg As String, add As Boolean)
    Dim joToolTip As JavaObject
    Dim joToolTip2 As JavaObject = joToolTip.InitializeNewInstance("javafx.scene.control.Tooltip", Array(msg))
    If add = True Then
        joToolTip.RunMethod("install", Array(iv, joToolTip2))
    Else
        joToolTip.RunMethod("uninstall", Array(iv, joToolTip2))
    End If
End Sub
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
Why on ImageViews only? So it works on any Node (and View).

B4X:
Private Sub ToolTipOnNode(Nd As Node, msg As String, add As Boolean)
    Dim joToolTip As JavaObject
    Dim joToolTip2 As JavaObject = joToolTip.InitializeNewInstance("javafx.scene.control.Tooltip", Array(msg))
    If add = True Then
        joToolTip.RunMethod("install", Array(Nd, joToolTip2))
    Else
        joToolTip.RunMethod("uninstall", Array(Nd, joToolTip2))
    End If
End Sub
 

LucaMs

Expert
Licensed User
Longtime User
Another version:
B4X:
Private Sub ToolTipOnNode(Nd As Node, msg As String, fnt As Font, iv As ImageView, add As Boolean)
   Dim joToolTip As JavaObject
   Dim joToolTip2 As JavaObject = joToolTip.InitializeNewInstance("javafx.scene.control.Tooltip", Array(msg))
   If add = True Then
       joToolTip.RunMethod("install", Array(Nd, joToolTip2))
   
       If fnt.IsInitialized Then
           joToolTip2.RunMethod("setFont", Array(fnt))
       End If

       If iv.IsInitialized Then
           joToolTip2.RunMethod("setGraphic", Array(iv))
           iv.Visible = True
       End If
   Else
       joToolTip.RunMethod("uninstall", Array(Nd, joToolTip2))
   End If

End Sub

1.gif
 
Top