Java Swing Font Size getSizeOfTextInComponent(String text, Font font, JComponent component)

Here you can find the source of getSizeOfTextInComponent(String text, Font font, JComponent component)

Description

returns the size of the text

License

Apache License

Parameter

Parameter Description
text a parameter
font a parameter

Return

new Dimension object with the size of the text

Declaration


public static Dimension getSizeOfTextInComponent(String text, Font font, JComponent component) 

Method Source Code

//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());
    }
}

Related

  1. getBaseFontSize()
  2. getCheckBoxBorder(int fontSize, boolean ltr)
  3. getDefaultFontSize()
  4. getFontRelativeSize(int size)
  5. getFontSizeFactor()
  6. increaseFontSize(JComponent component, int offset)
  7. printDocumentMonospaced(Graphics g, Document doc, int fontSize, int pageIndex, PageFormat pageFormat, int tabSize)
  8. printDocumentMonospacedWordWrap(Graphics g, Document doc, int fontSize, int pageIndex, PageFormat pageFormat, int tabSize)
  9. resetUIFont(String name, int style, int size)