Tur*_*g85 28 css java fxml javafx-8
我正在玩JavaFX Tooltip.我意识到,就个人而言,徘徊在某些东西和实际出现的工具提示之间的延迟太长了.API中的内容显示:
通常,当鼠标在控件上移动时,工具提示会"激活".工具提示变为"激活"和实际显示之间通常存在一些延迟.详细信息(例如延迟量等)留给Skin实现.
经过一些进一步的调查,我无法找到控制此行为的任何可能性.在JavaFX的CSS参考没有关于延迟时间和运行时间,评价信息getCssMetaData()没有帮助.
我知道,有一种方法可以通过onMouseEntered和手动控制工具提示onMouseExited,但是真的没有别的方法吗?或者我错过了一个明显的选择?
Igo*_*nov 42
我通过Reflection使用下一个hack
public static void hackTooltipStartTiming(Tooltip tooltip) {
try {
Field fieldBehavior = tooltip.getClass().getDeclaredField("BEHAVIOR");
fieldBehavior.setAccessible(true);
Object objBehavior = fieldBehavior.get(tooltip);
Field fieldTimer = objBehavior.getClass().getDeclaredField("activationTimer");
fieldTimer.setAccessible(true);
Timeline objTimer = (Timeline) fieldTimer.get(objBehavior);
objTimer.getKeyFrames().clear();
objTimer.getKeyFrames().add(new KeyFrame(new Duration(250)));
} catch (Exception e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
jew*_*sea 16
现有功能要求:JDK-8090477可自定义工具提示的可见性时间.
功能请求当前已安排集成到Java 9.附加到我链接的问题是一个可以应用的补丁,以允许您在早期Java版本中获得此功能.
您的另一个选择是使用以下技术之一创建自己的弹出控件:
我发现,通过上述实现,有时我仍然无法解释.
以下内容对我有用,并完全消除了延迟:
public static void bindTooltip(final Node node, final Tooltip tooltip){
node.setOnMouseMoved(new EventHandler<MouseEvent>(){
@Override
public void handle(MouseEvent event) {
// +15 moves the tooltip 15 pixels below the mouse cursor;
// if you don't change the y coordinate of the tooltip, you
// will see constant screen flicker
tooltip.show(node, event.getScreenX(), event.getScreenY() + 15);
}
});
node.setOnMouseExited(new EventHandler<MouseEvent>(){
@Override
public void handle(MouseEvent event){
tooltip.hide();
}
});
}
Run Code Online (Sandbox Code Playgroud)
在Java 9及更高版本中,您可以执行
Tooltip tooltip = new Tooltip("A tooltip");
tooltip.setShowDelay(Duration.seconds(3));
Run Code Online (Sandbox Code Playgroud)
还有一个hideDelay属性,用于显示工具提示与再次隐藏之间的延迟。的默认值为1秒,的默认值为showDelay200毫秒hideDelay。
在 JavaFx 9 中,可以通过 CSS 设置工具提示延迟。在样式表中执行此操作听起来很变态,但这可能就是他们所说的“细节(例如延迟量等)留给皮肤实现。”的意思。
https://docs.oracle.com/javase/9/docs/api/javafx/scene/doc-files/cssref.html#tooltip
所以你可以这样做:
.tooltip {
-fx-show-delay: 250ms;
}
Run Code Online (Sandbox Code Playgroud)
小智 5
扩展工具提示或放入应用程序类。(仅java8)
/**
* <p>
* Hack TooltipBehavior
*/
static {
try {
Tooltip obj = new Tooltip();
Class<?> clazz = obj.getClass().getDeclaredClasses()[1];
Constructor<?> constructor = clazz.getDeclaredConstructor(
Duration.class,
Duration.class,
Duration.class,
boolean.class);
constructor.setAccessible(true);
Object tooltipBehavior = constructor.newInstance(
new Duration(250), //open
new Duration(5000), //visible
new Duration(200), //close
false);
Field fieldBehavior = obj.getClass().getDeclaredField("BEHAVIOR");
fieldBehavior.setAccessible(true);
fieldBehavior.set(obj, tooltipBehavior);
}
catch (Exception e) {
Logger.error(e);
}
}
Run Code Online (Sandbox Code Playgroud)