Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / April 2006

Tip: Looking for answers? Try searching our database.

How to get line number of current cursor?

Thread view: 
JessyCute - 19 Apr 2006 05:11 GMT
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 - 19 Apr 2006 07:02 GMT
Perhaps this might help..
http://seminars.jguru.com/forums/view.jsp?EID=1247896

Regards,
Robert
Thomas Weidenfeller - 19 Apr 2006 09:39 GMT
> 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
Signature

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/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.