Example usage for javax.swing JTextArea getFontMetrics

List of usage examples for javax.swing JTextArea getFontMetrics

Introduction

In this page you can find the example usage for javax.swing JTextArea getFontMetrics.

Prototype

public FontMetrics getFontMetrics(Font font) 

Source Link

Document

Gets the FontMetrics for the specified Font.

Usage

From source file:Main.java

private static int getLineHeight(JTextArea textArea) {
    return textArea.getFontMetrics(textArea.getFont()).getHeight();
}

From source file:Main.java

public static int getTextableHeight(JTextArea textArea) {
    int textableHeight = textArea.getLineCount() * textArea.getFontMetrics(textArea.getFont()).getHeight();
    Insets insets = textArea.getInsets();
    if (insets != null)
        textableHeight += insets.top + insets.bottom;

    return textableHeight;
}

From source file:Main.java

/**
 * Source: http://stackoverflow.com/questions/102171/method-that-returns-the-line-number-for-a-given-jtextpane-position
 * Returns an int containing the wrapped line index at the given position
 * @param component JTextPane/* ww w  . j a v  a2  s  . co  m*/
 * @param int pos
 * @return int
 */
public static int getLineNumber(JTextArea component, int pos) {
    int posLine;
    int y = 0;

    try {
        Rectangle caretCoords = component.modelToView(pos);
        y = (int) caretCoords.getY();
    } catch (Exception ex) {
    }

    int lineHeight = component.getFontMetrics(component.getFont()).getHeight();
    posLine = (y / lineHeight);
    return posLine;
}

From source file:com.sander.verhagen.frame.LineWrapCellRenderer.java

private int getLineCount(JTextArea textArea) {
    AttributedString string = new AttributedString(textArea.getText());
    FontRenderContext fontRenderContext = textArea.getFontMetrics(textArea.getFont()).getFontRenderContext();
    AttributedCharacterIterator characterIterator = string.getIterator();
    LineBreakMeasurer lineBreakMeasurer = new LineBreakMeasurer(characterIterator, fontRenderContext);
    lineBreakMeasurer.setPosition(characterIterator.getBeginIndex());

    int lineCount = 0;
    while (lineBreakMeasurer.getPosition() < characterIterator.getEndIndex()) {
        lineBreakMeasurer.nextLayout(textArea.getSize().width);
        lineCount++;//from www .j a  va2  s.  c om
    }
    return lineCount;
}