Listening for Caret Movement Events in a JTextComponent - Java Swing

Java examples for Swing:JTextComponent

Description

Listening for Caret Movement Events in a JTextComponent

Demo Code

import javax.swing.JTextArea;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.text.JTextComponent;

public class Main {
  public void m() throws Exception {
    JTextComponent textComp = new JTextArea();
    textComp.addCaretListener(new CaretListener() {
      public void caretUpdate(CaretEvent e) {
        // dot is the caret position
        int dot = e.getDot();

        // mark is the non-caret end of the selection
        int mark = e.getMark();
      }/*from w  ww.j  a  v  a  2 s.com*/
    });
  }
}

Related Tutorials