I want to reduce the font size if the text is not suitable for JLabel

There were many reports on this issue, but I could not understand the answers that people gave there. Like in this post: “ How to change the font size of JLabel for maximum size ” the answer converts the font size to 14! But this is static in other answers; their entire output screen seems to be enlarging.

I show certain numbers in JLabel with the name "lnum", it can display numbers up to three digits, but after that it shows "4 ...". I want that if the number can fit in the label, it should not change the font size, but if it is 4 digits, it should reduce the font size so that it matches. NOTE. I do not want jLabel to resize. I just want to change the text in it.

Edit: Here is the code I tried

String text = lnum.getText(); System.out.println("String Text = "+text);//DEBUG Font originalFont = (Font)lnum.getClientProperty("originalfont"); // Get the original Font from client properties if (originalFont == null) { // First time we call it: add it originalFont = lnum.getFont(); lnum.putClientProperty("originalfont", originalFont); } int stringWidth = lnum.getFontMetrics(originalFont).stringWidth(text); int componentWidth = lnum.getWidth(); stringWidth = stringWidth + 25; //DEBUG TRY if (stringWidth > componentWidth) { // Resize only if needed // Find out how much the font can shrink in width. double widthRatio = (double)componentWidth / (double)stringWidth; int newFontSize = (int)Math.floor(originalFont.getSize() * widthRatio); // Keep the minimum size // Set the label font size to the newly determined size. lnum.setFont(new Font(originalFont.getName(), originalFont.getStyle(), newFontSize)); }else{ lnum.setFont(originalFont); // Text fits, do not change font size System.out.println("I didnt change it hahaha");//DEBUG } lnum.setText(text); 

I have a problem that many times it does not work, for example, if the text "-28885" shows "-28 ...".

stringWidth = stringWidth + 25; // DEBUG TRY

I had to add this code so that it would increase the length it got. This was the code that I added to temporarily fix the problem. I need a permanent solution for this.

+3
source share
1 answer

Adapted from the answer to the question you were talking about :

 void setTextFit(JLabel label, String text) { Font originalFont = (Font)label.getClientProperty("originalfont"); // Get the original Font from client properties if (originalFont == null) { // First time we call it: add it originalFont = label.getFont(); label.putClientProperty("originalfont", originalFont); } int stringWidth = label.getFontMetrics(originalFont).stringWidth(text); int componentWidth = label.getWidth(); if (stringWidth > componentWidth) { // Resize only if needed // Find out how much the font can shrink in width. double widthRatio = (double)componentWidth / (double)stringWidth; int newFontSize = (int)Math.floor(originalFont.getSize() * widthRatio); // Keep the minimum size // Set the label font size to the newly determined size. label.setFont(new Font(originalFont.getName(), originalFont.getStyle(), newFontSize)); } else label.setFont(originalFont); // Text fits, do not change font size label.setText(text); } 

When you select a number that matches, you must reset the font back to its original (see the else section).

EDIT: If you cannot / do not want to keep the link to the original font, you can save it as a “client property” (see first lines).

+2
source

All Articles