I'm aware of this thread showing a way to do but it leads to strange behaviors in my case and i'm wondering if there is no simpler way to do it.
I need to know the size my JTextArea
will have after setting the text. Here is how i do at the moment:
tarea.getLineCount() * tarea.getRowHeight();
It works unless there is no line wrapping. I'd like to do the same calculation with line wrapping. Is there any to know when a line wrapping happen? This way i would only need to increment the current line count by one.
EDIT:
Here is (maybe) a solution i found. It's almost copy paste of this by @camickr.
int rowStartOffset = textComponent.viewToModel(new Point(0, 0));
int endOffset = textComponent.viewToModel(new Point(0, textComponent.getHeight()));
int count = 0; //used to store the line count
while (rowStartOffset < endOffset) {
try {
rowStartOffset = Utilities.getRowEnd(textComponent, rowStartOffset) + 1;
} catch (BadLocationException ex) {
break;
}
count++;
}
I made few tests with/without line wrapping enabled and it seemed to work.