File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JavaFX and the fly likes Resizing text when labels overrun Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JavaFX
Bookmark "Resizing text when labels overrun" Watch "Resizing text when labels overrun" New topic
Author

Resizing text when labels overrun

Sam Rein
Greenhorn

Joined: Jul 15, 2013
Posts: 5
I am looking for a way to determine if a multiline label's text has overrun the label. JavaFX handles overrun by either clipping, or inserting whatever string you want for an ellipse. I want to adjust the font size of the string if the text overflows instead. The code would be easy enough, if only I could figure out how to detect if the text has overrun! From what I've researched, I there is no boolean property for this like there is for underline and wrapping, and there doesn't seem to be any way I can find to access the text's width and compare it to the labels. I've also tried just testing to see if there is an ellipse at the end of the label's string, but unsurprisingly that did not work since the ellipse is not added that way. I'd rather not use Text objects as I would lose some of label's features, but I guess the re-sizing would be more straight forward... Does anyone have any ideas for me?
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Have you tried testing whether the actual size is less than the preferred size, using the properties inherited from Control?


luck, db
There are no new questions, but there may be new answers.
Sam Rein
Greenhorn

Joined: Jul 15, 2013
Posts: 5
Darryl Burke wrote:Have you tried testing whether the actual size is less than the preferred size, using the properties inherited from Control?


Tried it just now, and it worked like a charm! The answer was right in front of me, I guess. I'm glad I asked before I did something I would have regret. Thanks!
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

You're welcome, and thank you for reporting back (I hadn't tested it myself). This solution may be useful to others who come across this thread, and that's what makes a forum a useful resource!
Sam Rein
Greenhorn

Joined: Jul 15, 2013
Posts: 5
Okay, so I've made a stupid mistake. I'm going to pin this one on a combination of excitement and a late night. It turns out that I botched the test, basically it doesn't work. Whoops. It looks like the string is clipped before the preferred size is set, so that the preferred width is never larger than the actual width, but it can be smaller or equal to. I'm not sure why this would be the case though. It seems odd that the preferred width would be calculated after the content is modified to fit the max size.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

In that case you could try iteratively adjusting the font size until the preferredSize *is* smaller than the actual size.
Sam Rein
Greenhorn

Joined: Jul 15, 2013
Posts: 5
Wouldn't that shrink the text size until it only fits on one line though? Actually, maybe the preferred height is what expands when the text overflows, since it goes to a new line... I'll test these when I can get to my computer.
Sam Rein
Greenhorn

Joined: Jul 15, 2013
Posts: 5
So I've tested it, and it seems that my problem is that the preferred width and height are ALWAYS -1. I can't seem to find an explanation for this in the control documentation either. I assume -1 means that the preferred width is auto-calculated based on the content, but I'm not sure how to get the number it comes up with. The actual measurements I can get easily.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Ouch. If this had been Swing, I would have searched for a solution in the source code. But with FX, that's not available.
I assume -1 means that the preferred width is auto-calculated
Totally reasonable assumption.
John Damien Smith
Ranch Hand

Joined: Jan 26, 2012
Posts: 100
    
    7
Ouch. If this had been Swing, I would have searched for a solution in the source code. But with FX, that's not available.


Full source code of JavaFX (with the exception of the media subsystem which is not relevant to this topic) is available. The master repository for JavaFX 8 is here, doing a mercurial checkout or clicking on the zip link at the top of the linked page will download the latest source to our machine.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Resizing text when labels overrun
 
Similar Threads
SWT: access to controls from outside main
HTML brings in problems on Layout.
Liskov Schmiskov
Image in JLabel
use user's input in JTextField in a class as a label for JLabel, how to do that?