How to change the size of the font of a JLabel to take the maximum size
I have a
JLabel in a Container.
The defaut size of the font is very small.
I would like that the text of the
JLabel to take the maximum size.
How can I do that?
Not the most pretty code, but the following will pick an appropriate font size for a
label such that the text inside will fit the interior as much as possible without overflowing the label:
Font labelFont = label.getFont(); String labelText = label.getText(); int stringWidth = label.getFontMetrics(labelFont).stringWidth(labelText); int componentWidth = label.getWidth(); // Find out how much the font can grow in width. double widthRatio = (double)componentWidth / (double)stringWidth; int newFontSize = (int)(labelFont.getSize() * widthRatio); int componentHeight = label.getHeight(); // Pick a new font size so it will not be larger than the height of label. int fontSizeToUse = Math.min(newFontSize, componentHeight); // Set the label's font size to the newly determined size. label.setFont(new Font(labelFont.getName(), Font.PLAIN, fontSizeToUse));
Basically, the code looks at how much space the text in the
JLabel takes up by using the
FontMetrics object, and then uses that information to determine the largest font size that can be used without overflowing the text from the
The above code can be inserted into perhaps the
paint method of the
JFrame which holds the
JLabel, or some method which will be invoked when the font size needs to be changed.
The following is an screenshot of the above code in action:
Read more... Read less...
JLabel textLabel = new JLabel("<html><span style='font-size:20px'>"+Text+"</span></html>");
Source Code for Label - How to change Color and Font (in Netbeans)
jLabel1.setFont(new Font("Serif", Font.BOLD, 12)); jLabel1.setForeground(Color.GREEN);
Just wanted to point out that the accepted answer has a couple of limitations (which I discovered when I tried to use it)
- As written, it actually keeps recalculating the font size based on a ratio of the previous font size... thus after just a couple of calls it has rendered the font size as much too large. (eg Start with 12 point as your DESIGNED Font, expand the label by just 1 pixel, and the published code will calculate the Font size as 12 * (say) 1.2 (ratio of field space to text) = 14.4 or 14 point font. 1 more Pixel and call and you are at 16 point !).
It is thus not suitable (without adaptation) for use in a repeated-call setting (eg a
ComponentResizedListener, or a custom/modified
The listed code effectively assumes a starting size of 10 pt but refers to the current font size and is thus suitable for calling once (to set the size of the font when the label is created). It would work better in a multi-call environment if it did
int newFontSize = (int) (widthRatio * 10); rather than
int newFontSize = (int)(labelFont.getSize() * widthRatio);
Because it uses
new Font(labelFont.getName(), Font.PLAIN, fontSizeToUse))to generate the new font, there is no support for Bolding, Italic or Color etc from the original font in the updated font. It would be more flexible if it made use of
The solution does not provide support for HTML label Text. (I know that was probably not ever an intended outcome of the answer code offered, but as I had an HTML-text
JPanelI formally discovered the limitation. The
FontMetrics.stringWidth()calculates the text length as inclusive of the width of the html tags - ie as simply more text)
I recommend looking at the answer to this SO question where trashgod's answer points to a number of different answers (including this one) to an almost identical question. On that page I will provide an additional answer that speeds up one of the other answers by a factor of 30-100.
JLabel label = new JLabel("Hello World"); label.setFont(new Font("Calibri", Font.BOLD, 20));