How to get line number of current cursor?  
Author Message
JessyCute





PostPosted: 2006-4-19 12:12:00 Top

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.

 
Robert Blixt





PostPosted: 2006-4-19 14:03:00 Top

java-programmer >> How to get line number of current cursor? Perhaps this might help..
http://seminars.jguru.com/forums/view.jsp?EID=1247896


Regards,
Robert

 
Thomas Weidenfeller





PostPosted: 2006-4-19 16:39:00 Top

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/