B4J Code Snippet Add a ToolTip to an ImageView

Discussion in 'B4J Code Snippets' started by rwblinn, Apr 5, 2016.

  1. rwblinn

    rwblinn Well-Known Member Licensed User

    Hi,

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

    Code:
    '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:
    Code:
    Private imgTMP36 As ImageView
    ToolTipToImageView(imgTMP36, 
    "The TMP36 temperature sensor")
     
  2. Cableguy

    Cableguy Expert Licensed User

    Made a very small change to allow removing the tooltip .

    Code:
    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: Oct 27, 2016
    Mashiane and rwblinn like this.
  3. LucaMs

    LucaMs Expert Licensed User

    Why on ImageViews only? So it works on any Node (and View).

    Code:
    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
     
  4. LucaMs

    LucaMs Expert Licensed User

    Another version:
    Code:
    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
     
    Erel likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice