Java Font Text Height getStringHeight(Component c)

Here you can find the source of getStringHeight(Component c)

Description

Returns the height of the given component.

License

LGPL

Parameter

Parameter Description
c the component where the text is contained

Return

the height of the text

Declaration

public static int getStringHeight(Component c) 

Method Source Code


//package com.java2s;
/*//w ww  .  j  a v a2s  .  c o  m
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

import java.awt.*;

public class Main {
    /**
     * Returns the height of the given component.
     *
     * @param c the component where the text is contained
     * @return the height of the text
     */
    public static int getStringHeight(Component c) {
        // get metrics from the graphics
        FontMetrics metrics = c.getFontMetrics(c.getFont());
        // get the height of a line of text in this font and render context
        int hgt = metrics.getHeight();
        // calculate the height of a box to hold the text with some padding.
        return hgt + 2;
    }
}

Related

  1. getStringDrawingHeight(Graphics graphics, String string)
  2. getStringHeight(Graphics2D g, String str)
  3. getStringHeight(Graphics2D graphics2D)
  4. getTextHeight(String s, FontMetrics fm, Graphics g)
  5. getTextHeight(String text, Graphics g, String newLineSplit)