Counting Lines

How do I count the number of lines in a JTextArea component using a JButton to tell it when to update the count?

Cosmo439Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
tomboshellConnect With a Mentor Commented:
Have the button retrieve the value returned by JTextArea's 'getLineCount()' method, which returns an int.  

Otherwise, a work around could be first call the Document, then get the elements and send it to an ElementIterator and count.

Document doc = txtA.getDocument();
int ctr=0;
ElementIterator elmIt=new ElementIterator(doc.getDefaultRootElement());
while((elmIt.next() != null)){
   ctr++;
}

something like the code above would be a work-around, but try the getLineCount() method first if it is a JTextArea
0
 
tomboshellCommented:
Thanks and I hope that it was a help!
Tom
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.