10 Replies Latest reply on Mar 5, 2012 6:57 PM by Richard Bair-Oracle

    Tooltip in fxml howto

    917878
      Hi All,

      I am trying to define tooltip in fxml (javaFX 2.0), but no success:

      The code below compiles, but the tooltip does not show. Should I activate it somehow?
      If I add activated="true" an error (Property "activated" does not exist or is read-only) comes up.
      ...
      <ImageView fx:id="action0" fitHeight="48" fitWidth="48" 
          onMouseClicked="#Action">
          <tooltip>
              <Tooltip text="Ho-hó-mmm"/>
          </tooltip>
          <style>
              -fx-effect: dropshadow( two-pass-box, black, 10, 0.0, 0, 0)
          </style>
          <image>
              <Image url="@icons/home_48.png"/>
          </image>
      </ImageView>
      ...
      I tried to wrap IamgeView into a Button, but got the same result.
        • 1. Re: Tooltip in fxml howto
          885691
          ImageView isn't a Control, so it doesn't define a "tooltip" property. Try wrapping it in a ScrollPane (which is a Control) and set the tooltip on that:
          <ScrollPane fitToWidth="true" fitToHeight="true">
              <tooltip>
                  <Tooltip text="Ho-hó-mmm"/>
              </tooltip>
              <ImageView fitHeight="48" fitWidth="48">
                  <style>
                      -fx-effect: dropshadow( two-pass-box, black, 10, 0.0, 0, 0)
                  </style>
                  <image>
                      <Image url="@icons/home_48.png"/>
                  </image>
              </ImageView>
          </ScrollPane>
          • 2. Re: Tooltip in fxml howto
            jsmith
            Only being able to install Tooltips on Controls rather than nodes makes Tooltip installation via FXML less flexible than it's Java counterpart.
            Placing something in a ScrollPane so that it can have a Tooltip installed is a workable solution, but it's a bit clunky.
            • 3. Re: Tooltip in fxml howto
              885691
              This has nothing to do with FXML - the Node class does not define a setTooltip() method, so you wouldn't be able to do this in Java either.

              In most cases, if a property is accessible in Java, it is also accessible in FXML.
              • 4. Re: Tooltip in fxml howto
                jsmith
                Tooltip has static install and uninstall methods which allow you to place and remove a Tooltip on any node:
                http://docs.oracle.com/javafx/2.0/api/javafx/scene/control/Tooltip.html#install%28javafx.scene.Node,%20javafx.scene.control.Tooltip%29
                http://docs.oracle.com/javafx/2.0/api/javafx/scene/control/Tooltip.html#uninstall%28javafx.scene.Node,%20javafx.scene.control.Tooltip%29
                • 5. Re: Tooltip in fxml howto
                  885691
                  I had forgotten about those methods. And you are correct that you can't access them from markup, because FXML does not provide a way to invoke static methods (nor should it, since it is fundamentally an instance declaration language, not a programming language).

                  To me, it seems like setTooltip() should be moved up to Node. As a developer, that's where I'd expect to find it - not in a static utility method on Tooltip. Then it would be accessible from FXML as well.
                  • 6. Re: Tooltip in fxml howto
                    jsmith
                    In most cases, if a property is accessible in Java, it is also accessible in FXML.
                    Yep.
                    because FXML does not provide a way to invoke static methods (nor should it, since it is fundamentally an instance declaration language, not a programming language).
                    Yep.
                    To me, it seems like setTooltip() should be moved up to Node. As a developer, that's where I'd expect to find it - not in a static utility method on Tooltip. Then it would be accessible from FXML as well.
                    I think so too. When I first started using JavaFX I had to dig to find the Tooltip install API and IDE autocomplete on Node doesn't show it up either.

                    Perhaps the current install design was so that the SceneGraph does not have a compile dependency on the controls library - not sure of the reason, but the static install and uninstall methods on Tooltip seem like a strange API.

                    To track this, I created http://javafx-jira.kenai.com/browse/RT-20070 "Add instance methods to Node for adding and removing a Tooltip".
                    • 7. Re: Tooltip in fxml howto
                      Jonathan Giles-Oracle
                      jsmith wrote:

                      Perhaps the current install design was so that the SceneGraph does not have a compile dependency on the controls library - not sure of the reason, but the static install and uninstall methods on Tooltip seem like a strange API.

                      To track this, I created http://javafx-jira.kenai.com/browse/RT-20070 "Add instance methods to Node for adding and removing a Tooltip".
                      This is correct, we can't have the scenegraph depending on the controls code. One option is to move the Tooltip class into the scenegraph project (javafx-ui-common), or to have a parent Tooltip class put into the scenegraph and have the controls impl extend from it. Anywho, it is something I'd like to see resolved as well. I've reassigned your bug report to Paru, who is the owner of the Tooltip API.

                      -- Jonathan
                      • 8. Re: Tooltip in fxml howto
                        Richard Bair-Oracle
                        Jonathan Giles wrote:
                        jsmith wrote:

                        Perhaps the current install design was so that the SceneGraph does not have a compile dependency on the controls library - not sure of the reason, but the static install and uninstall methods on Tooltip seem like a strange API.

                        To track this, I created http://javafx-jira.kenai.com/browse/RT-20070 "Add instance methods to Node for adding and removing a Tooltip".
                        This is correct, we can't have the scenegraph depending on the controls code. One option is to move the Tooltip class into the scenegraph project (javafx-ui-common), or to have a parent Tooltip class put into the scenegraph and have the controls impl extend from it. Anywho, it is something I'd like to see resolved as well. I've reassigned your bug report to Paru, who is the owner of the Tooltip API.
                        Even if you move Tooltip to javafx-ui-common, you also need to move Control and Skin over. I think everybody would agree that having setTooltip on Node seems more natural, but it violated modularity. We felt that the number of times you wanted to put a tooltip on a non-control was probably minimal, so it was better to leave the API on Control rather than move it to Node and introduce this dependency.

                        Also, ImageView isn't resizable, and sometimes people wish it were.

                        So the thought was that perhaps we ought to add an ImageWell control, which would be resizable (with all the scaling / resizing / repeat / etc features one would expect, as well as drag out support and optional edit ability with drag on etc), as well as having a tooltip. I suspect that would be the better solution.

                        Richard
                        • 9. Re: Tooltip in fxml howto
                          917878
                          Thanks for all of you for the exhaustive answer! I did learn much from your contributions.

                          I actually did wrap (this time) successfully the ImageView into a Button. That works for me.

                          I would find useful an ImageView having tooltip. In my case for the application launcher I just would like to show 10 icons and then fire up the actions associated for the one clicked. However the need to wrap it into a Button control is "bearable sacrifice".
                          • 10. Re: Tooltip in fxml howto
                            Richard Bair-Oracle
                            Actually, this is the right thing to do! Buttons are actually more complicated than a simple ImageView in terms of the click behavior, and using an ImageView in this scenario would not be best in this case. For example, ImageView doesn't handle focus by default, so it is not accessible. Also, it doesn't have support for handling "space" vs. "enter" etc in a cross platform manner. So I would encourage the use of a Button for this type of usage.

                            Maybe if we had an "ImageButton" that is more what you would be expecting I guess.