Example usage for javax.swing JTextArea modelToView

List of usage examples for javax.swing JTextArea modelToView

Introduction

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

Prototype

@Deprecated(since = "9")
public Rectangle modelToView(int pos) throws BadLocationException 

Source Link

Document

Converts the given location in the model to a place in the view coordinate system.

Usage

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/*from   w ww. j ava 2 s .  c  o 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;
}