Java tutorial
import javax.swing.JFrame; import javax.swing.JTextPane; import javax.swing.event.CaretEvent; import javax.swing.event.CaretListener; import javax.swing.text.Highlighter.Highlight; public class Main extends JFrame { public Main() { setSize(300, 200); setDefaultCloseOperation(EXIT_ON_CLOSE); final JTextPane textPane = new JTextPane(); add(textPane); textPane.addCaretListener(new CaretListener() { public void caretUpdate(CaretEvent e) { Highlight[] h = textPane.getHighlighter().getHighlights(); for (int i = 0; i < h.length; i++) { System.out.println(h[i].getStartOffset()); System.out.println(h[i].getEndOffset()); } } }); } public static void main(String args[]) { new Main().setVisible(true); } }