This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Columns vs letters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Columns vs letters" Watch "Columns vs letters" New topic
Author

Columns vs letters

Craig Parsons
Ranch Hand

Joined: Jan 28, 2004
Posts: 40
I have a JtextArea that is 42 Columns long. When enough letters are entered by the user to fill that line of the textarea I want to count it.
How can I determine how many letters will fit on the line? If I use getWidth it gives me the length in pixles. GetColums will return the number of columns, which won't do much good.
I am able to count the number of characters have been entered using insertUpdate but am not sure how to determine when the end of line has been reached.
any ideas?
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
The problem is that when tab is pressed only a tab character is inserted but the caret is advanced a number of spaces. Now the document has only one character more but the caret in the text area is nearer the right side of the component more than what would correspond to having typed only one character. That is, there is a mismatch between the douments length and the visible position of the caret. Otherwise we could use textArea.getCaretPosition or caretEvent.getDot to find out if the caret is at the end of the line.
This snippet uses another approach

In the case tabs are not used within the textArea set the tab size to 0 and use the first technique.


SCJP2. Please Indent your code using UBB Code
Craig Parsons
Ranch Hand

Joined: Jan 28, 2004
Posts: 40
Thanks, that was a big help.
Thanks again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Columns vs letters
 
Similar Threads
count occurrence of number in array
Assignment Java-1b (Resolved)
determine the number of rows to be saved from a table to an excel sheet
Another way to get the information needed
Counting specific word using tokenizer