?
Solved

Counting Lines

Posted on 2002-07-15
2
Medium Priority
?
193 Views
Last Modified: 2010-03-31
How do I count the number of lines in a JTextArea component using a JButton to tell it when to update the count?

0
Comment
Question by:Cosmo439
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 7

Accepted Solution

by:
tomboshell earned 200 total points
ID: 7156443
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
 
LVL 7

Expert Comment

by:tomboshell
ID: 7168750
Thanks and I hope that it was a help!
Tom
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month10 days, 6 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question