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 / General / September 2003

Tip: Looking for answers? Try searching our database.

JTextArea: get last line of text

Thread view: 
Aaron Boxer - 29 Sep 2003 15:33 GMT
Hello,

I have subclassed a JTextArea, and am handling the keyboard events.
I would like to get the last line of text each time the user
presses carriage return. I am going to use the widget as a
serial communication terminal: send each line of text over the serial
port after CR.

Any help would be greatly appreciated.

Aaron Boxer
Daniel Sjöblom - 29 Sep 2003 17:21 GMT
> Hello,
>
[quoted text clipped - 3 lines]
> serial communication terminal: send each line of text over the serial
> port after CR.

Look up getLineOfOffset and getLineEnd/StartOffset in the documentation.
Signature

Daniel Sjöblom

Bill Tschumy - 29 Sep 2003 17:37 GMT
> Hello,
>
[quoted text clipped - 7 lines]
>
> Aaron Boxer

I haven't compiled this, but this should be pretty close to what you want.

Document document = textArea.getDocument();
Element rootElem = document.getDefaultRootElement();
int numLines = rootElem.getElementCount();
Element lineElem = rootElem.getElement(numLines - 1);
int lineStart = lineElem.getStartOffset();
int lineEnd = lineElem.getEndOffset();
String lineText = document.getText(lineStart, lineEnd - lineStart);

Signature

Bill Tschumy
Otherwise -- Austin, TX
http://www.otherwise.com

Aaron Boxer - 30 Sep 2003 14:34 GMT
Hi Bill,

thanks for your reply!

I just had to change one line, because of zero offsets:

Element lineElem = rootElem.getElement(numLines - 2);

Cheers,

Aaron

> > Hello,
> >
[quoted text clipped - 17 lines]
> int lineEnd = lineElem.getEndOffset();
> String lineText = document.getText(lineStart, lineEnd - lineStart);


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



©2009 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.