Mega Code Archive

 
Categories / Java Tutorial / Swing
 

Listening for Caret Movement Events in a JTextComponent

import javax.swing.JTextArea; import javax.swing.event.CaretEvent; import javax.swing.event.CaretListener; import javax.swing.text.JTextComponent; public class Main {   public static void main(String[] argv) {     JTextComponent textComp = new JTextArea();     textComp.addCaretListener(new CaretListener() {       public void caretUpdate(CaretEvent e) {         int dot = e.getDot();         System.out.println("dot is the caret position:" + dot);         int mark = e.getMark();         System.out.println("mark is the non-caret end of the selection: " + mark);       }     });   } }