Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test for Label overrun

I have a multiline label whose text has a chance of overrunning. If it does this, I want to decrease the font size until it isn't overrunning, or until it hits some minimum size. This would hopefully make it so that the label will change size until the entire string is visible.

My problem it that I am not sure how to test to see if the text has overrun. I have tried testing to see if the label's text ends with the ellipse string, but I believe the ellipse is not technically added to the textProperty of the label. So does anyone know of a good way to test for this?

like image 670
SwR Avatar asked Jul 12 '13 18:07

SwR


1 Answers

minisu posted a way to detect an overrun in this answer: https://stackoverflow.com/a/15178908/9492864

This way works for all labeled and I tested it on Buttons with JavaFX 8. You can add a listener for example to the needsLayoutProperty:

labeled.needsLayoutProperty().addListener((observable, oldValue, newValue) -> {
   String originalString = labeled.getText();
   Text textNode = (Text) labeled.lookup(".text"); // "text" is the style class of Text
   String actualString = textNode.getText();

   boolean clipped = !actualString.isEmpty() && !originalString.equals(actualString);

   System.out.println("is " + originalString + " clipped: " + clipped);
});
like image 150
Johann Alfred Verdant Anderson Avatar answered Sep 19 '22 19:09

Johann Alfred Verdant Anderson