JavaFX: How to implement a Custom Tooltip

ぃ、小莉子 提交于 2019-12-11 14:07:31

问题


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.

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:


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

Default tooltip:

Customized tooltip:

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

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:

  • Pane Shape Modification


来源:https://stackoverflow.com/questions/48173943/javafx-how-to-implement-a-custom-tooltip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!