Get Current Caret Position in JTextField - Java Swing

Java examples for Swing:JTextField

Description

Get Current Caret Position in JTextField

Demo Code


import java.awt.FlowLayout;
 
import javax.swing.JApplet;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
 
/*//from  w  ww. ja v a  2  s.  co m
 * Implement CaretListener in order to listen for caret position
 * change event.
 */
public class Main implements CaretListener {
       
        public void init(){
                JTextField field = new JTextField("JTextField Caret Position Example");
                field.addCaretListener(this);
        }
 
        public void caretUpdate(CaretEvent e) {
                int position = e.getDot();
               
        }
}

Related Tutorials