JavaFX:如何实现自定义工具提示

时间:2022-09-02 15:09:38

I'm trying to create my own Tooltip class. Basically the tooltip is going to be a Label with a pointer graphic(FontAwesomeView) on the bottom all wrapped inside a vbox.

我正在尝试创建自己的Tooltip类。基本上工具提示将是一个标签,底部的指针图形(FontAwesomeView)都包含在vbox中。

The part i'm stuck at is, how do i show my custom tooltip on the screen without adding it to a parent node?

我坚持的部分是,如何在屏幕上显示我的自定义工具提示而不将其添加到父节点?

1 个解决方案

#1


3  

You don't need to implement your own tooltip, you can just customize the built-in one.

您不需要实现自己的工具提示,只需自定义内置工具提示即可。

Default tooltip:

JavaFX:如何实现自定义工具提示

Customized tooltip:

JavaFX:如何实现自定义工具提示

It also works for installing Tooltips on nodes (like shapes) which don't have a setTooltip method:

它也适用于在没有setTooltip方法的节点(如形状)上安装Tooltips:

JavaFX:如何实现自定义工具提示

Sample code for customized tooltip:

自定义工具提示的示例代码:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.PopupWindow;
import javafx.stage.Stage;

public class BubbleTip extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    private static final String SQUARE_BUBBLE =
            "M24 1h-24v16.981h4v5.019l7-5.019h13z";

    @Override
    public void start(Stage stage) {
        Label label = new Label("hello,");
        label.setStyle("-fx-font-size: 16px;");

        label.setTooltip(makeBubble(new Tooltip(" world")));

        Circle circle = new Circle(20, Color.AQUA);
        Tooltip.install(circle, makeBubble(new Tooltip("circle")));

        VBox layout = new VBox(10, label, circle);
        layout.setPadding(new Insets(20));

        stage.setScene(new Scene(layout));
        stage.show();
    }

    private Tooltip makeBubble(Tooltip tooltip) {
        tooltip.setStyle("-fx-font-size: 16px; -fx-shape: \"" + SQUARE_BUBBLE + "\";");
        tooltip.setAnchorLocation(PopupWindow.AnchorLocation.WINDOW_BOTTOM_LEFT);

        return tooltip;
    }
}

This answer is based on the answer to:

这个答案是基于以下答案:

#1


3  

You don't need to implement your own tooltip, you can just customize the built-in one.

您不需要实现自己的工具提示,只需自定义内置工具提示即可。

Default tooltip:

JavaFX:如何实现自定义工具提示

Customized tooltip:

JavaFX:如何实现自定义工具提示

It also works for installing Tooltips on nodes (like shapes) which don't have a setTooltip method:

它也适用于在没有setTooltip方法的节点(如形状)上安装Tooltips:

JavaFX:如何实现自定义工具提示

Sample code for customized tooltip:

自定义工具提示的示例代码:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.PopupWindow;
import javafx.stage.Stage;

public class BubbleTip extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    private static final String SQUARE_BUBBLE =
            "M24 1h-24v16.981h4v5.019l7-5.019h13z";

    @Override
    public void start(Stage stage) {
        Label label = new Label("hello,");
        label.setStyle("-fx-font-size: 16px;");

        label.setTooltip(makeBubble(new Tooltip(" world")));

        Circle circle = new Circle(20, Color.AQUA);
        Tooltip.install(circle, makeBubble(new Tooltip("circle")));

        VBox layout = new VBox(10, label, circle);
        layout.setPadding(new Insets(20));

        stage.setScene(new Scene(layout));
        stage.show();
    }

    private Tooltip makeBubble(Tooltip tooltip) {
        tooltip.setStyle("-fx-font-size: 16px; -fx-shape: \"" + SQUARE_BUBBLE + "\";");
        tooltip.setAnchorLocation(PopupWindow.AnchorLocation.WINDOW_BOTTOM_LEFT);

        return tooltip;
    }
}

This answer is based on the answer to:

这个答案是基于以下答案: