0

I need to know the size of a text using a textlayout object. I found the following bunch of codes

final TextLayout LAYOUT = Toolkit.getToolkit().getTextLayoutFactory().createLayout();
LAYOUT.setContent(text != null ? text : "", font.impl_getNativeFont());
LAYOUT.setLineSpacing(1.0f);
LAYOUT.setWrapWidth(100.0f);
LAYOUT.setBoundsType(TextLayout.BOUNDS_CENTER);
return LAYOUT.getBounds().getHeight();

The code is working properly except I have a warning message concerning getNativeFont which seems to be deprecated, knowing that what I need is the height of the text

So my question : What is the appropriate method ?

Thanks in advance !

AnthonyCFE
  • 71
  • 11
  • TextLayout is a `com.sun.javafx.scene.text` class. Anything API or class in the `com.sun.javafx` package is not publicly supported API and is not guaranteed to be usable in future Java versions. So using TextLayout isn't really recommended unless you have no other choice and you can also control the version of the JavaFX runtime which your application runs on to ensure that it is a compatible one with your application. – jewelsea Mar 21 '17 at 22:24
  • You could try the techniques listed in: [How to calculate the pixel width of a String in JavaFX?](http://stackoverflow.com/questions/13015698/how-to-calculate-the-pixel-width-of-a-string-in-javafx) and see if that works for you. – jewelsea Mar 21 '17 at 22:25

1 Answers1

1

Thank you,

It is working for me:

Text theText = new Text(theLabel.getText());
theText.setFont(theLabel.getFont());
double width = theText.getBoundsInLocal().getWidth();
AnthonyCFE
  • 71
  • 11