Here you can find the source of biggestFont(final javax.swing.text.JTextComponent c)
Parameter | Description |
---|---|
c | the JTextComponent for which to get the biggest font. |
public static Font biggestFont(final javax.swing.text.JTextComponent c)
//package com.java2s; //License from project: Open Source License import java.awt.Font; public class Main { /**/*from ww w. ja v a 2 s .com*/ * Gets the biggest font that will fit in the text component. * @param c the {@link JTextComponent} for which to get the biggest font. * @return The biggest {@link Font} for JTextComponent c */ public static Font biggestFont(final javax.swing.text.JTextComponent c) { Font labelFont = c.getFont(); String labelText = c.getText(); int stringWidth = c.getFontMetrics(labelFont).stringWidth(labelText); int componentWidth = c.getWidth(); // Find out how much the font can grow in width. double widthRatio = (double) componentWidth / (double) stringWidth; int newFontSize = (int) Math.floor(labelFont.getSize() * widthRatio); int componentHeight = c.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. return new Font(labelFont.getName(), labelFont.getStyle(), fontSizeToUse); } }