List of usage examples for javax.swing JTextArea modelToView
@Deprecated(since = "9") public Rectangle modelToView(int pos) throws BadLocationException
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; }