Last active
March 26, 2018 17:36
-
-
Save darmbrust/9559744d1b1dada434a3 to your computer and use it in GitHub Desktop.
A utility method to allow setting the global timing parameters on tooltips in JavaFX (Java 8)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.Constructor; | |
import java.lang.reflect.Field; | |
import javafx.scene.control.Tooltip; | |
import javafx.util.Duration; | |
import org.slf4j.LoggerFactory; | |
/** | |
* {@link ToolTipDefaultsFixer} | |
* | |
* @author <a href="mailto:[email protected]">Dan Armbrust</a> | |
*/ | |
public class ToolTipDefaultsFixer | |
{ | |
/** | |
* 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) | |
{ | |
LoggerFactory.getLogger(ToolTipDefaultsFixer.class).error("Unexpected", e); | |
return false; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very nice fix for nagging problem.