如何将工具提示添加到JavaFX Canvas的矩形区域 [英] How do I add a Tooltip to a rectangular region of a JavaFX Canvas

查看:93
本文介绍了如何将工具提示添加到JavaFX Canvas的矩形区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的JavaFX应用程序中,我有一个TableView,其中有多列,其中一列以图形形式显示数据.为此,我创建了一个CanvasCell对象,该对象创建并管理自己的Canvas以处理图形.绘图部分效果很好.

In my JavaFX app I have a TableView with multiple columns, one of which displays data in a graphical form. To do this I have created a CanvasCell object that creates and manages its own Canvas to deal with the drawing. The drawing part works just fine.

我现在想将Tooltips放在Canvas/Cell中的某些区域上.每个Cell可能有多个Tooltips(这使我无法在Cell级别添加Tooltip),并且它们仅应在图形的特定区域中触发.但是,我根本无法使其正常运行.我似乎不太了解显示节点层次结构的交互作用(完全阅读),无法将Tooltip放置在实际起作用的任何地方.

I'd now like to put Tooltips over some regions within the Canvas/Cell. There may be multiple Tooltips per Cell (which prevents me from adding the Tooltip at the Cell level) and they should only trigger in specific regions of the graph. However, I'm not managing to get it functioning at all. I don't seem to understand the interactions of Display Node hierarchy well enough (read "at all") to be able to place the Tooltip anywhere where it will actually work.

JavaFX的文档稀疏,对于我尝试过的所有搜索,Google + SO都为空白.有没有人知道如何做这种事情,或者我现在应该把它写成不是一种选择".

Documentation for JavaFX is sparse and Google + SO has come up blank for all searches that I've tried. Is there anyone who knows how to do this sort of thing or should I just write it off as "not an option" for now.

有关信息,CanvasCell调用updateItem()上扩展的Canvas对象内的draw()函数.我尝试创建Tooltip的代码位于该draw()函数内部,如下所示:

For info, the CanvasCell calls a draw() function inside an extended Canvas object on updateItem(). The code in which I've tried to create a Tooltip sits inside that draw() function and looks like:

    Rectangle rect = new Rectangle(leftVal, topVal, width, height);
    gc.fillRect(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
    Tooltip tooltip = new Tooltip("Tooltip Text");
    Tooltip.install(rect, tooltip);

但是该代码的编写比其他任何事情都寄予希望,并且不会在界面中产生任何有用的信息.

but that code was written more in hope than anything else and doesn't generate anything useful in the interface.

如果有人可以指出正确的方向,我将不胜感激.

If someone can point me in the right direction, I will be very grateful.

推荐答案

如果您不需要在此处所示的时序控制,,您只需安装 放在随附的 并利用

If you don't need the timing control illustrated here, you can simply install the Tooltip on the enclosing Canvas and leverage Shape::contains to condition the text as shown below.

node.setOnMouseMoved(e -> {
    tooltips.forEach((color, bounds) -> {
        if (bounds.contains(e.getX(), e.getY())) {
            tooltip.setText(color.toString());
        }
    });
});

按照建议的此处,Java 9和更高版本提供了对

As suggested here, Java 9 and later provide control over Tooltip timing via the properties showDelay and showDuration.

此处展示了类似的方法.

import javafx.application.Application;
import javafx.scene.shape.Rectangle;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.StackPane;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.stage.Stage;

import java.util.HashMap;
import java.util.Map;

/**
 * @see https://stackoverflow.com/a/53785468/230513
 * @see https://stackoverflow.com/a/53753537/230513
 */
public class CanvasTooltipDemo extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        StackPane root = new StackPane();
        Scene sc = new Scene(root, 400, 400);
        stage.setScene(sc);
        Canvas canvas = new Canvas(200, 200);
        root.getChildren().add(canvas);

        Map<Color, Rectangle> tooltips = new HashMap<>();
        tooltips.put(Color.RED, new Rectangle(0, 0, 100, 100));
        tooltips.put(Color.BLUE, new Rectangle(100, 0, 100, 100));
        tooltips.put(Color.YELLOW, new Rectangle(0, 100, 100, 100));
        tooltips.put(Color.GREEN, new Rectangle(100, 100, 100, 100));
        GraphicsContext gc = canvas.getGraphicsContext2D();
        tooltips.forEach((color, bounds) -> {
            gc.setFill(color);
            gc.fillRect(bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight());
        });

        setToolTips(canvas, tooltips);
        stage.show();
    }

    private void setToolTips(Node node, Map<Color, Rectangle> tooltips) {
        Tooltip tooltip = new Tooltip();
        Tooltip.install(node, tooltip);
        node.setOnMouseMoved(e -> {
            tooltips.forEach((color, bounds) -> {
                if (bounds.contains(e.getX(), e.getY())) {
                    tooltip.setText(color.toString());
                }
            });
        });
        node.setOnMouseExited(e -> {
            tooltip.hide();
        });
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}

这篇关于如何将工具提示添加到JavaFX Canvas的矩形区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆