?
Solved

access specific positions in JTextArea

Posted on 2001-06-14
11
Medium Priority
?
367 Views
Last Modified: 2008-02-01
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.
0
Comment
Question by:menubar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
11 Comments
 
LVL 92

Expert Comment

by:objects
ID: 6191069
The mthod you need is setCaretPosition(ion position) where position is the character position in the document.
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 :)

0
 

Author Comment

by:menubar
ID: 6192689
It seems that for my purpose, I'll do:

str=textArea.getText();
index=str.indexOf(strToFind);
textArea.setSelectStart(index);

?

0
 
LVL 2

Accepted Solution

by:
JonathanJonas earned 200 total points
ID: 6193577
When you have found the index/offset of the position that you wish to scroll to you can use a line like

textArea.setCaretPosition( index );

This will automatically scroll so that the caret is visible on the screen. The only problem with that is that if the line is already on the screen it is not obvious that it has been found.

The solution is to make your text area scroll so that the found line is always at the top of the screen. (Or as close to the top as possible)

You achieve this by first scrolling to the bottom of the area, then scrolling back to the cursor position. To perform this scrolling use the scrollRectToVisible(Rectangle rect) method from JComponent.

You could use code something like this:

// Scroll the caret and the view to the found line,
// so that the line appears at the top of the screen.
textArea.setCaretPosition( index );
//Get the end of the document                
end = jsDoc.getLength() - 1;
Rectangle endCoords = editPane.modelToView( end );
textArea.scrollRectToVisible( endCoords );
                 
Rectangle rect = editPane.modelToView( index );
textArea.scrollRectToVisible( rect );


Cheers,

Jonathan

0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 2

Expert Comment

by:JonathanJonas
ID: 6193584
Sorry - those references to editPane.modelToView should read: textArea.modelToView();

Jonathan
0
 

Author Comment

by:menubar
ID: 6203103
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);
0
 

Author Comment

by:menubar
ID: 6203107
sorry, the line containing "Error" CANNOT be seen without scrolling down.
0
 

Author Comment

by:menubar
ID: 6203244
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
0
 

Author Comment

by:menubar
ID: 6203253
sorry, I mean: how do I highlight the line?
0
 
LVL 2

Expert Comment

by:JonathanJonas
ID: 6204593
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(lineHighlightColor);
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(lineHighlightColor);
    gfx.fillRect(0,y,getWidth(),height);
}

0
 
LVL 2

Expert Comment

by:JonathanJonas
ID: 6204617
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
0
 
LVL 2

Expert Comment

by:JonathanJonas
ID: 6204627
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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month9 days, 20 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question