developers

    Forum Stats

  • 3,873,876 Users
  • 2,266,626 Discussions
  • 7,911,649 Comments

Discussions

Showing a tooltip on labeled controls, if text is truncated

csh
csh Member Posts: 525
edited May 2, 2016 7:15AM in JavaFX 2.0 and Later

Hi,

I like to display a tooltip on Labeled controls, but only if their text is displayed truncated with an ellipsis.

After studying the source code, I saw that there is a Text element in the skin (at least for Label). I could easily compare the text of the skin with the text of the control and display a tooltip if they are not equal.

I need to create a binding, which listens to the skinProperty, its children and to the textProperty of the first Text node of this skin.

I've started something like this:

label.tooltipProperty().bind(new ObjectBinding<Tooltip>() {

  {

   super.bind(label.skinProperty());

  }

   @Override
   protected Tooltip computeValue() {

   SkinBase<?> skin = (SkinBase<?>) label.getSkin();

   if (skin != null) {

   for (Node node : skin.getChildren()) {

   if (node instanceof Text) {

   return new Tooltip(((Text) node).getText());

  }

  }

  }

   return null;

  }

});

But the tooltip obviously won't update if skin.getChildren() changes or the textProperty() of Text.

I somehow need to create an ObjectBinding, which triggers on a) skin changes, b) children changes, c) text changes.

Any ideas how to do that?

This discussion has been closed.
developers