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?
This work for me !! In javafx
Only call a asignarTexto(label, texto) for set text a label and check if the text is overrun in the label then add a tooltip for label.
The short and disappointing answer: You simply cannot do this in a reliable way.
The slightly longer answer is, that the label itself does not even know whether it's overflown or not. Whenever a label is resized, the skin class (
LabeledSkinBase
) is responsible for updating the displayed text. This class, however, uses a JavaFX utils class to compute the ellipsoided text. The problem here is that the respective method just returns a string that is ellipsoid if this is required by the label's dimensions. The skin itself never gets informed about whether the text was actually ellipsoided or not, it just updates the displayed text to the returned result.What you could try is to check the displayed text of the skin class, but it's protected. So you would need to do is to subclass
LabelSkin
, and implement something like that:If you use this skin for you Label, you should be able to detect whether your text is ellipsoided. If you wonder about the loop and the reflection: Java didn't allow me to access the text field by other means, so this may be a strong indicator that you really should not do this ;-) Nevertheless: It works!
Disclaimer: I've only checked for JavaFX 8
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: