首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何向不使用FXML实现控件的节点添加工具提示?

如何向不使用FXML实现控件的节点添加工具提示?
EN

Stack Overflow用户
提问于 2016-04-30 03:59:06
回答 1查看 6.2K关注 0票数 8

在JavaFX中,控制的子类有一个tooltip属性,这意味着可以使用FXML声明地添加工具提示

代码语言:javascript
运行
复制
<CheckBox fx:id="someCheckBox" mnemonicParsing="false" text="Do It?">
    <tooltip>
        <Tooltip text="Whether or not to do it."/>
    </tooltip>
</CheckBox>

这是可能的,因为CheckBox是Control的子类。

但是,我希望能够使用FXML向任何场景图节点添加工具提示(即不必使用Java编程添加工具提示)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-30 03:59:06

Oracle上有一个归档讨论,它建议将节点封装在ScrollPane中并在ScrollPane上设置工具提示。虽然这并不理想,因为它为场景图添加了一个不必要的节点,但更重要的是,并不是所有场景都能很好地工作。考虑将工具提示添加到TitledPane的图形中

代码语言:javascript
运行
复制
<TitledPane text="My TitledPane" fx:id="someTitledPane" expanded="true" contentDisplay="RIGHT">
    <graphic>
        <ScrollPane fitToWidth="true" fitToHeight="true" style="-fx-background-color: rgba(255, 255, 255, 0);">
            <ImageView fitWidth="24.0" fitHeight="24.0" preserveRatio="true" pickOnBounds="true">
            <image>
                <Image url="/images/questionMark.jpg" />
            </image>
            </ImageView>
        <tooltip>
            <Tooltip text="My tooltip."/>
        </tooltip>
        </ScrollPane>
    </graphic>
    <Label text="Hello!"/>
</TitledPane>

尽管ScrollPane有一个透明的背景,但在问号图形后面仍然有一个可见的白色框:

有一个解决方法,那就是利用<fx:script>的力量

代码语言:javascript
运行
复制
<?language javascript?>

<TitledPane text="My TitledPane" fx:id="someTitledPane" expanded="true" contentDisplay="RIGHT">
    <graphic>
        <ImageView fx:id="questionMarkImageView" fitWidth="24.0" fitHeight="24.0" preserveRatio="true" pickOnBounds="true">
        <image>
            <Image url="/images/questionMark.jpg" />
        </image>
        </ImageView>
        <fx:script>
            var tooltip = new javafx.scene.control.Tooltip('My tooltip');
            javafx.scene.control.Tooltip.install(questionMarkImageView, tooltip);
        </fx:script>
    </graphic>
    <Label text="Hello!"/>
</TitledPane>

请注意,fx:id of ImageView是在脚本中引用的(我在任何地方都没有看到这个功能,只是通过实验才发现的--这实际上促使我发布这个Q&A,希望其他人会发现它有用)。结果如下:

票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36950542

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档