Method that returns the line number for a given JTextPane position?

Question!

I'm looking for a method that computes the line number of a given text position in a JTextPane with wrapping enabled.

Example:

This a very very very very very very very very very very very very very very very very very very very very very very long line.
This is another very very very very very very very very very very very very very very very very very very very very very very long line.|

The cursor is on line number four, not two.

Can someone provide me with the implementation of the method:

int getLineNumber(JTextPane pane, int pos)
{
     return ???
}


Answers

http://java-sl.com/tip_row_column.html An alternative which works with text fragments formatted with different styles



Try this

 /**
   * Return an int containing the wrapped line index at the given position
   * @param component JTextPane
   * @param int pos
   * @return int
   */
  public int getLineNumber(JTextPane component, int pos) 
  {
    int posLine;
    int y = 0;

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

    int lineHeight = component.getFontMetrics(component.getFont()).getHeight();
    posLine = (y / lineHeight) + 1;
    return posLine;
  }
By : Richard


All we do is we create a branch. We have the standard root level directories: trunk, tags, releases, branches.

The main thing to remember is that all branching is simply like creating a copy, and all branches off of the trunk are just like creating a copy (except that it is a shallow copy, only copying the deltas).

For us, all development is done in the trunk. If someone is doing a major rework then tend to put it in branches. Major releases are put into releases and all other labels and items we want to tag are put in the tags folder.

For our releases, we have the following directory structure:

repository
+--trunk
+--releases
  +--v1.0
  +--v1.1
  +--v1.4
  +--v2.0
+--branches
+--tags
By : Xetius


This video can help you solving your question :)
By: admin