Here you can find the source of getSizeOfTextInComponent(String text, Font font, JComponent component)
Parameter | Description |
---|---|
text | a parameter |
font | a parameter |
public static Dimension getSizeOfTextInComponent(String text, Font font, JComponent component)
//package com.java2s; //License from project: Apache License import java.awt.Dimension; import java.awt.Font; import java.awt.FontMetrics; import javax.swing.JComponent; import sun.swing.SwingUtilities2; public class Main { /**//from ww w .jav a2 s . co m * returns the size of the text * * @param text * @param font * @return new Dimension object with the size of the text */ public static Dimension getSizeOfTextInComponent(String text, Font font, JComponent component) { FontMetrics fontMetrics = SwingUtilities2.getFontMetrics(component, font); return new Dimension(fontMetrics.stringWidth(text), fontMetrics.getHeight()); } }