0

I'm trying to change my JLabel text size using this code:

my_font = my_label.getFont();

my_font = my_font.deriveFont(
        Collections.singletonMap(
                TextAttribute.SIZE, TextAttribute.20));

There is a compiler warning on this line:

TextAttribute.20;

This code works perfectly and changes the text weight:

font_bold = numero_fattura.getFont();

font_bold = font_bold.deriveFont(
        Collections.singletonMap(
                TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD));

Question: How do I use the same code to change the text size?

jwpfox
  • 5,124
  • 11
  • 45
  • 42
Niccolò Segato
  • 96
  • 2
  • 13

1 Answers1

1

As I understand your question, your answer is similar to this below said thread first answer, just follow this link -

How to change the size of the font of a JLabel to take the maximum size

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));

hope this will help you, thanks.

ArifMustafa
  • 4,617
  • 5
  • 40
  • 48