Reputation: 3874
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?
Upvotes: 93
Views: 410798
Reputation: 1
The method I made fixes all of the problems with most popular solution on this thread (I think). If you have other HTML commands that you use—I only use center—you can remove them from the String before splitting it as well. If you want this to work for JButtons, just change the parameter to JButton and it will work. You can put this method in a repeater to continuously update font and it works.
public void maxFont(JLabel label){
String sin = label.getText();
String[] ss = sin.replace("<html>","").replace("<center>", "").split("<br>");
int lines = ss.length;
String s="";
FontMetrics fm = getFontMetrics(new JLabel().getFont());
int maxWidth=0;
if(!sin.equals(ss[0])){
for (String sss : ss) {
int p = fm.stringWidth(sss);
if(p>maxWidth){
maxWidth=p;
s=sss;
}
}
}else {
s=sin;
maxWidth=fm.stringWidth(s);
}
int newWidth = (label.getWidth()/maxWidth)*10;
int maxHeight = label.getHeight()*7/8/lines;
int fontSize = Math.min(newWidth,maxHeight);
label.setFont(new Font(label.getFont().getName(), Font.PLAIN, fontSize));
}
Upvotes: 0
Reputation: 135
I tried this its worked for me:
Label welcomeMsg = new JLabel("Contact us");
welcomeMsg.setBounds(200,10,100,25);
welcomeMsg.setFont( new Font("Serif",Font.PLAIN,22));
Upvotes: 0
Reputation: 71
JLabel label = new JLabel("Hello World");
label.setFont(new Font("Calibri", Font.BOLD, 20));
Upvotes: 7
Reputation: 160954
Not the most pretty code, but the following will pick an appropriate font size for a JLabel
called 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 JLabel
.
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:
(source: coobird.net)
Upvotes: 90
Reputation: 217
Just wanted to point out that the accepted answer has a couple of limitations (which I discovered when I tried to use it)
It is thus not suitable (without adaptation) for use in a repeated-call setting (eg a ComponentResizedListener
, or a custom/modified LayoutManager
).
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 labelFont.deriveFont
instead.
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 JLabel
on my JPanel
I 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.
Upvotes: 2
Reputation: 41
Source Code for Label - How to change Color and Font (in Netbeans)
jLabel1.setFont(new Font("Serif", Font.BOLD, 12));
jLabel1.setForeground(Color.GREEN);
Upvotes: 4
Reputation: 1544
JLabel textLabel = new JLabel("<html><span style='font-size:20px'>"+Text+"</span></html>");
Upvotes: 10
Reputation: 3693
label = new JLabel("A label");
label.setFont(new Font("Serif", Font.PLAIN, 14));
taken from How to Use HTML in Swing Components
Upvotes: 125