我正在尝试创建自己的工具提示实现,该实现带有箭头,并在其安装节点的特定一侧显示工具提示。 看起来像这样:
这是我到目前为止编写的代码
Tooltip.java
public class Tooltip extends PopupControl {
private StringProperty message = new SimpleStringProperty();
private final static TooltipSkin skin = new TooltipSkin();
private final static TooltipBehavior behavior = new TooltipBehavior();
public Tooltip(Node node, String message) {
this.message.setValue(message);
if (getContent() != skin.createSkin()) {
getContent().setAll(skin.createSkin());
}
behavior.install(node, Tooltip.this);
}
/****************************************************************
********** ACCESSORS ************
****************************************************************/
String getMessage() {
return message.get();
}
StringProperty messageProperty() {
return message;
}
private static class TooltipBehavior {
final String TOOLTIP_PROP_KEY = "fxtooltip";
private void install(Node node, Tooltip tooltip) {
if (node == null)
return;
node.addEventHandler(MouseEvent.MOUSE_ENTERED, ENTERED_HANDLER);
node.addEventHandler(MouseEvent.MOUSE_EXITED, KILL_HANDLER);
node.addEventHandler(MouseEvent.MOUSE_PRESSED, KILL_HANDLER);
node.getProperties().put(TOOLTIP_PROP_KEY, tooltip);
}
private EventHandler<MouseEvent> ENTERED_HANDLER = (MouseEvent event) -> {
Node hoveredNode = (Node) event.getSource();
Tooltip tooltip = ((Tooltip) hoveredNode.getProperties().get(TOOLTIP_PROP_KEY));
String message = tooltip.getMessage();
skin.setMessage(message);
tooltip.show(getWindow(hoveredNode), 0, 0);
};
private EventHandler<MouseEvent> KILL_HANDLER = (MouseEvent event) -> {
Node hoveredNode = (Node) event.getSource();
Tooltip tooltip = ((Tooltip) hoveredNode.getProperties().get(TOOLTIP_PROP_KEY));
tooltip.hide();
};
private Window getWindow(final Node node) {
final Scene scene = node == null ? null : node.getScene();
return scene == null ? null : scene.getWindow();
}
}
private static class TooltipSkin {
private static HBox $RootView = new HBox();
private static StackPane $PaneMessageBody = new StackPane();
private static StackPane $PaneArrow = new StackPane();
private static Text $TextMessage = new Text();
private static boolean skinInitialized = false;
Pane createSkin() {
if (skinInitialized) {
return $RootView;
} else {
skinInitialized = true;
$RootView.getChildren().addAll($PaneMessageBody, $PaneArrow);
$PaneMessageBody.getChildren().addAll($TextMessage);
}
return $RootView;
}
void setMessage(String message) {
$TextMessage.setText(message);
}
}
}
在我的控制器中,我有一些按钮,我以此方式添加了工具提示
new Tooltip($BtnDownload, "Download");
new Tooltip($BtnActivate, "Activate");
但是,这仅在安装了工具提示的最后一个节点上有效,如果我将鼠标悬停在具有工具提示的其他节点上,光标开始闪烁,并且工具提示完全不显示。问题可能在这里吗?