menubar
asked on
access specific positions in JTextArea
Is there a way to position cursor at (or highlight) a specific line in a JTextArea (e.g., a line that contains certain string)? If so, how?
For example, I have a textArea embeded in a scrollPane. There is a 'find' button with a string entry. User can enter and search for a specific string in the textArea. When the string is found, the textArea should scroll automatically to show the found string.
For example, I have a textArea embeded in a scrollPane. There is a 'find' button with a string entry. User can enter and search for a specific string in the textArea. When the string is found, the textArea should scroll automatically to show the found string.
ASKER
It seems that for my purpose, I'll do:
str=textArea.getText();
index=str.indexOf(strToFin d);
textArea.setSelectStart(in dex);
?
str=textArea.getText();
index=str.indexOf(strToFin
textArea.setSelectStart(in
?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry - those references to editPane.modelToView should read: textArea.modelToView();
Jonathan
Jonathan
ASKER
How come the following few line do not work? The line with the string "Error" is seen without scrolling down. The code program does not bring the line into scene. The string is found at index=122. Do I need to implement Caret or some other interfaces?
String str=textArea.getText();
int index=str.indexOf("Error") ;
textArea.setCaretPosition( index);
String str=textArea.getText();
int index=str.indexOf("Error")
textArea.setCaretPosition(
ASKER
sorry, the line containing "Error" CANNOT be seen without scrolling down.
ASKER
JJ,
I was able to bring the line into view using your code. However, I do now know what the variable "jsDoc" in your code is.
Also, how do I hight the line?
Thanks
I was able to bring the line into view using your code. However, I do now know what the variable "jsDoc" in your code is.
Also, how do I hight the line?
Thanks
ASKER
sorry, I mean: how do I highlight the line?
Hi,
The variable jsDoc was referring to the document associated with that text area. You can get it using textArea.getDocument()
I was just using the document to get the length of the text.
To highlight the line you can do it a couple of different ways, the way I've done it before is to use the java.awt.Graphics class to highlight the line when repaint() is called on the component.
The code looks a bit like this:
gfx.setColor(lineHighlight Color);
gfx.fillRect(0,y,getWidth( ),height);
You would probably want some logic around this call so that the line is only highlighted when there is no other selection of text on that line - otherwise they override each other.
Something like:
int selectionStart = textArea.getSelectionStart ();
int selectionEnd = textArea.getSelectionEnd() ;
if(selectionStart == selectionEnd)
{
gfx.setColor(lineHighlight Color);
gfx.fillRect(0,y,getWidth( ),height);
}
The variable jsDoc was referring to the document associated with that text area. You can get it using textArea.getDocument()
I was just using the document to get the length of the text.
To highlight the line you can do it a couple of different ways, the way I've done it before is to use the java.awt.Graphics class to highlight the line when repaint() is called on the component.
The code looks a bit like this:
gfx.setColor(lineHighlight
gfx.fillRect(0,y,getWidth(
You would probably want some logic around this call so that the line is only highlighted when there is no other selection of text on that line - otherwise they override each other.
Something like:
int selectionStart = textArea.getSelectionStart
int selectionEnd = textArea.getSelectionEnd()
if(selectionStart == selectionEnd)
{
gfx.setColor(lineHighlight
gfx.fillRect(0,y,getWidth(
}
In the above code to fill the rectangle, I calculated the height of the line using the font metrics class.
Like so:
fm = new FontMetrics( font );
int height = fm.getHeight();
Cheers,
Jonathan
Like so:
fm = new FontMetrics( font );
int height = fm.getHeight();
Cheers,
Jonathan
FontMetrics is the old school way of doing it - it's actually deprecated - I'm not sure what the new way to do it is.
*shrugs*
Cheers,
Jonathan
*shrugs*
Cheers,
Jonathan
If you want to position at a line that contains a certain string then you will need to determine the character position of the line in question.
Hope this helps , let me know if u need a hand :)