只要鼠标悬停在工具提示上,就保持工具提示打开 [英] Keep Tooltip open as long as mouse is over it

查看:250
本文介绍了只要鼠标悬停在工具提示上,就保持工具提示打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过 fxml TableColumn 标题创建工具提示 c>像这样:

I create a Tooltip for a TableColumn header via fxml like this:

<TableColumn>
    <cellValueFactory>
        <PropertyValueFactory property="someProperty" />
    </cellValueFactory>
    <graphic>
        <Label text="Column 1">
            <tooltip>
                <Tooltip text="Tooltip text" />
            </tooltip>
        </Label>
    </graphic>
</TableColumn>

如果我将鼠标移到工具提示上,我想保持工具提示打开。最后我想在工具提示文本中有可点击的链接(就像Eclipse JavaDoc工具提示一样)。
这可能吗?

I would like to keep the tooltip open if I move the mouse over the tooltip. Eventually I would like to have clickable links in the tooltip text (Just like Eclipse JavaDoc tooltips). Is that possible?

编辑
考虑到答案,我现在尝试以下方法:

Edit: Considering the answer, I am trying the following now:

Label label = new Label();
label.setText("test text");
DelayedTooltip beakerTip = new DelayedTooltip();
beakerTip.setDuration(3000);
beakerTip.setText("Science from Base: 12");
beakerTip.isHoveringTarget(label);
Tooltip tooltip = new Tooltip();
tooltip.setText("test tooltip text");
label.setTooltip(beakerTip);
myTableColumn.setGraphic(label);

这里的问题是标签是与Tooltip不同。因此,如果鼠标位于工具提示之上但未超过标签,则隐藏工具提示。我无法将Tooltip本身作为悬停目标传递,因为它不是 Node

Here the problem is that the label is not the same as the Tooltip. So if the mouse is over the Tooltip but not over the label, the Tooltip is hidden. I cannot pass the Tooltip itself as a hover target, since it is not a Node.

推荐答案

我现在使用这个类,它按预期工作:

I use this class now and it works as expected:

public class HoveringTooltip extends Tooltip {

    private Timer timer = new Timer();
    private Map<Object, Boolean> mapHoveringTarget2Hovering = new ConcurrentHashMap<>();
    private final int duration;

    public HoveringTooltip(int duration) {
        super.setAutoHide(false);
        this.duration = duration;
    }

    public void addHoveringTarget(Node object) {

        mapHoveringTarget2Hovering.put(object, false);
        object.setOnMouseEntered(e -> {
            onMouseEntered(object);
        });
        object.setOnMouseExited(e -> {
            onMouseExited(object);
        });
    }

    public void addHoveringTarget(Scene object) {

        mapHoveringTarget2Hovering.put(object, false);
        object.setOnMouseEntered(e -> {
            onMouseEntered(object);
        });
        object.setOnMouseExited(e -> {
            onMouseExited(object);
        });
    }

    @Override
    public void hide() {

        // super.hide();
    }

    public boolean isHovering() {

        return isHoveringProperty().get();
    }

    public BooleanProperty isHoveringProperty() {

        synchronized(mapHoveringTarget2Hovering) {
            for(Entry<Object, Boolean> e : mapHoveringTarget2Hovering.entrySet()) {
                if(e.getValue()) {
                    // if one hovering target is hovering, return true
                    return new ReadOnlyBooleanWrapper(true);
                }
            }
            // no hovering on any target, return false
            return new ReadOnlyBooleanWrapper(false);
        }
    }

    private synchronized void onMouseEntered(Object object) {

        // System.err.println("Mouse entered for " + object + ", canelling timer");
        // stop a potentially running hide timer
        timer.cancel();
        mapHoveringTarget2Hovering.put(object, true);
    }

    private synchronized void onMouseExited(Object object) {

        // System.err.println("Mouse exit for " + object + ", starting timer");
        mapHoveringTarget2Hovering.put(object, false);
        startTimer();
    }

    private void startTimer() {

        timer.cancel();
        timer = new Timer();
        timer.schedule(new TimerTask() {

            @Override
            public void run() {

                Platform.runLater(new Runnable() {

                    @Override
                    public void run() {

                        if(!isHovering())
                            HoveringTooltip.super.hide();
                    }
                });
            }
        }, duration);
    }
}

这篇关于只要鼠标悬停在工具提示上,就保持工具提示打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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