java-programmer, How to get line number of current cursor?
I try to get the line number of current that cursor is on from the
JEditorPane.
I found method getSelectionStart() and getSelectedText(), but it not
has the getSelectedLine() or ...
So, anyone know how to get the current line number that cursor is on.
Thanks.
java-programmer >> How to get line number of current cursor?
JessyCute wrote:
> I try to get the line number of current that cursor is on from the
> JEditorPane.
> I found method getSelectionStart() and getSelectedText(), but it not
> has the getSelectedLine() or ...
This depends on your document. E.g. if you use a
javax.swing.text.PlainDocument, then it should be easy:
JEditorPane editor = ...
PlainDocument doc = (PlainDocument) editor.getDocument();
int offset = editor.getCaretPosition();
int lineNbr = doc.getDefaultRootElement().getElementIndex(offset);
Other document types behave totally different. E.g. HTMLDocument builds
a hierarchical Element structure where each Element represents a
structural block of HTML, and not just a line of text.
/Thomas
--
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/