I have a JTextArea
with a fixed size and the JTextArea
is allowed to wrap the lines and words.
I'm trying to get the last word before the JTextArea
break the line.
Is there a way to get it?
I already try to use lineCount()
, getStartOffset()
, getEndOffset()
and getRows()
. I know how long my text is, how many characters it has. lineCount
always return 1
.
I was thinking about a change to get the possible character length for the JTextArea
, or to find the break informations in the string, but there are no methods for it.
Did anyone has a Idea to get it?
Here is some untested code to give you an idea.
Since you know the size of the text area you can use the model/view methods of the text area to get the text.
int width = textArea.getSize().width;
int height = textArea.modelToView(0).height / 2;
int offset = textArea.viewToModel( new Point(width, height) );
int start = Utilities.getPreviousWord( offset ); // or maybe getWordStart(...)?
int end = Utilitities.getWordEnd( start );
String text = textArea.getText(start, end - start);