B4J Code Snippet Configure Tooltip timings

Description: I have found that the tooltip delay is too long most of the time, searching google I found this:
https://gist.github.com/darmbrust/9559744d1b1dada434a3
This is a straight implementation of this code.

Code:
Full code module: Create a new code module and copy the code to there, or just copy the subs and java code to an existing code module:

B4X:
'Static code module
Sub Process_Globals
    Private fx As JFX

End Sub

'Configure tooltip behaviour
Public Sub ConfigureTooltip(OpenDelay As Long,VisibleDuration As Long,CloseDelay As Long)

    Dim MEJO As JavaObject = Me
    Dim ClassShortName As String = MEJO.RunMethod("toString",Null)
    ClassShortName = ClassShortName.SubString(ClassShortName.LastIndexOf("."))
    Dim ModJO As JavaObject
    ModJO.InitializeStatic(GetPackageName(Me) & ClassShortName)

    If ModJO.RunMethod("setTooltipTimers",Array(OpenDelay, VisibleDuration, CloseDelay)) = False  Then
        Log($"*******${CRLF}Tooltip Configuration failed${CRLF}*******"$)
    End If

End Sub

'Return the current PackageName
Private Sub GetPackageName(Module As Object) As String
    Dim ModJo As JavaObject = Module
    Dim MoStr As String = ModJo.RunMethod("toString",Null)
    Return MoStr.SubString2(MoStr.LastIndexOf(" ")+1,MoStr.LastIndexOf("."))
End Sub

#if java

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import javafx.scene.control.Tooltip;
import javafx.util.Duration;

  /**
   * Returns true if successful.
   * Current defaults are 1000, 5000, 200;
   */
  @SuppressWarnings({ "rawtypes", "unchecked" })
  public static boolean setTooltipTimers(long openDelay, long visibleDuration, long closeDelay)
  {
    try
    {
      Field f = Tooltip.class.getDeclaredField("BEHAVIOR");
      f.setAccessible(true);


      Class[] classes = Tooltip.class.getDeclaredClasses();
      for (Class clazz : classes)
      {
        if (clazz.getName().equals("javafx.scene.control.Tooltip$TooltipBehavior"))
        {
          Constructor ctor = clazz.getDeclaredConstructor(Duration.class, Duration.class, Duration.class, boolean.class);
          ctor.setAccessible(true);
          Object tooltipBehavior = ctor.newInstance(new Duration(openDelay), new Duration(visibleDuration), new Duration(closeDelay), false);
          f.set(null, tooltipBehavior);
          break;
        }
      }
    }
    catch (Exception e)
    {

      return false;
    }
    return true;
  }


#end if

Call it before showing the MainForm in your main module, it will apply to all tooltips in the application.

Code module is called Utils for this example:

B4X:
Sub AppStart (Form1 As Form, Args() As String)

    'Call it before Showing the main form
    Utils.ConfigureTooltip(200,5000,500)

    MainForm = Form1
    MainForm.RootPane.LoadLayout("1") 'Load the layout file.
    MainForm.Show

End Sub

Tags: B4j Tooltip time delay timing
 

Attachments

  • configuretooltip.zip
    2.9 KB · Views: 380
Last edited:

AHilton

Active Member
Licensed User
Longtime User
I have been using this with a few of my desktop apps for a few years and today it started crashing the apps. Removing this call, alone, fixes the problem. It's only doing it on some computers and I believe it might have to do with the java update to the client machines from over the weekend.

Here are the computers' java jre version and Windows version that seems to be the common denominator...

Java 1.8.0_341
Win10 Pro 21H2
 

stevel05

Expert
Licensed User
Longtime User
What error is displayed when it crashes? Please post the Log entries
 
Last edited:
Top