Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to color a particular sentence in JEditorPane.

 
Padma Malladi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am having problems with coloring a sentence in a JEditorPane. I am doing the following: I have a JEditorPane which displays a HTML text. When the user moves his cursor over a sentence I need to highlight the sentence(only) with yellow color. I could get upto the point where I am extracting the sentence from the line where the mouse is moved. But I can't figure out as to how to color that sentence. Here's the code for the getting the sentence.
MouseMotionListener mouseMotionListener=new MouseMotionAdapter() {
public void mouseMoved(MouseEvent e) {
x = e.getX(); y = e.getY();
Point p = new Point(x, y);
AccessibleContext ac = editorPane.getAccessibleContext();
AccessibleText at = ac.getAccessibleText();
int index = at.getIndexAtPoint(p);
String str = at.getAtIndex(AccessibleText.SENTENCE,
index);
//I NEED TO HIGLIGHT THIS SENTENCE IN THE EDITORPANE.
}
};
I've looked into various methods available for JEditorPane, among which select(int selectionStart,int selectionEnd) seems to be helpful. If I use this, I can later use the setSelectionColor(Color c) method to highlighten that selected sentence. But I can't figure out what values I should send to the method select, to have that particular sentence selected. Please help me with this. Thanks a lot.
Padma
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Padma,
After looking at the problem for a little while I have found something that will work for you. You just add the following lines below your comment.

The method getText was required because I have noticed that in Windows the string that gets returned from editorPane.getText() includes some '\r' characters (carriage return). Add the AccessibleText doesn't include these so using it to compare would fail if the '\r' chars were not handled. The getText method just removes the '\r' chars from the textPane string.

Enjoy,
Manfred.
 
Padma Malladi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Manfred,
Thanks a lot for the help. But, it is not working like expected. I mean, irrespective of where the mouse is placed on the editorpane, it just selects the first few lines of the html text in the editorpane. I didn't mention this before but I am loading a html file into editorpane dynamically(using the JEditorPane contructor, editorPane = new JEditorPane(url); and the url being like http://www.rbi.com). Thus, the firstIndex is always -1(the first character in the html text) and depending on the length of the str, the lastIndex is calculated. The str selected, from the editorPane using AccessibleContext and AccessibleText, is correct although the firstIndex is wrong. I hope you can still help me with this problem. Thank You.
Padma
 
Padma Malladi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Manfred,
I realized why firstIndex is always equal to -1. This is because the method indexOf(str) is not able to find this string str in the editorPane text and hence is returning -1. And the reason why indexOf(str) cannot recognize the string str is because, the string returned by the method getText() has all kinds of html tags within it. Hence indexOf method is not able to match the str string with the one in the editorPane string. I am not sure how to solve this though??
Padma
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic