javaswingmouseoverjtextpanestyleddocument

How to use onmouseover in StyledDocumend assosiated with a JTextPane


I want to highlight different sections of text when mouse is hovered over them. I'm currently using styleddocument with jTextPane. Can somebody please help me in defining style so that individual strings/paragraphs can be highlighted when mouseover occurs?

Thanks,


Solution

  • You can define a custom highlighter to show necessary colors. Add a MouseListener to the jTextPane. You can get mouse coordinates and use viewToModel() method of jTextPane to detect offset in the Document and process whether current section should be highlighted or not.