Search code examples
javaswingfontsjtextfieldfont-size

Maximize font size while keeping text within JTextField


I have a JTextField which may vary in size depending on resolution. The JTextField will hold a 3 letter string.

I want to set the font size of the JTextField in such a way to MAXIMIZE the font size while still having the text FIT inside of the JTextField perfectly.

Is there an algorithm to do this?


Solution

  • I have used Martijn's answer, along with the following answers:

    String length in pixels in Java

    Java: Getting a font with a specific height in pixels

    ...in order to write a full answer to my question. Here it goes. Thanks to all who contributed.

    You will need to import the following:

    import javax.swing.JTextField;
    import java.awt.Font;
    import java.awt.image.BufferedImage;
    import java.awt.FontMetrics;
    import java.lang.Math;
    

    .................................

    public int getFontSize(JTextField text, int columnsToHold){
                //Create a sample test String (we will it later in our calculations)
                String testString = "";
                for(int i = 0; i<columnsToHold; i++){
                      testString = testString + "5";
                }
    
    
                 //size will hold the optimal Vertical point size for the font
          Boolean up = null;
          int size = text.getHeight();  
          Font font;
         while (true) {
            font = new Font("Default", 0, size);
            int testHeight = getFontMetrics(font).getHeight();
            if (testHeight < height && up != Boolean.FALSE) {
                size++;
                up = Boolean.TRUE;
            } else if (testHeight > height && up != Boolean.TRUE) {
                size--;
                up = Boolean.FALSE;
            } else {
               break;
            }
        }
            //At this point, size holds the optimal Vertical font size
    
            //Now we will calculate the width of the sample string
        BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
        FontMetrics fm = img.getGraphics().getFontMetrics(font);
        int width = fm.stringWidth(testString);
    
            //Using Martijn's answer, we calculate the optimal Horizontal font size
        int newFontSize = size * textos[0].getWidth()/width;
    
            //The perfect font size will be the minimum between both optimal font sizes.
            //I have subtracted 2 from each font so that it is not too tight to the edges
        return Math.min(newFontSize-2, size-2);
    }