File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Columns vs letters" Watch "Columns vs letters" New topic

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:
subject: Columns vs letters
It's not a secret anymore!