public class CaretSample { public static void main(String args[]) {
JFrame frame = new JFrame("Caret Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextArea textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
frame.add(scrollPane, BorderLayout.CENTER);
CaretListener listener = new CaretListener() { public void caretUpdate(CaretEvent caretEvent) {
System.out.println("Dot: "+ caretEvent.getDot());
System.out.println("Mark: "+caretEvent.getMark());
}
};