How to Get Current Caret Position in JTextField

Below code demonstrates how to get current caret position in a JTextField import java.awt.FlowLayout; import javax.swing.JFrame; import...

Below code demonstrates how to get current caret position in a JTextField

import java.awt.FlowLayout;
 
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
public class JTextFieldCaretPosition extends JFrame  implements CaretListener{
      JTextFieldCaretPosition()
      {
          this.getContentPane().setLayout(new FlowLayout());
               
                //create new JTextField
                JTextField field = new JTextField("JTextField Caret Position Example");
               
                add(field);
               
                //add CaretListener in order to listen for caret position changes
                field.addCaretListener(this);
                setSize(300,200);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                setVisible(true);
                
        }
 
        //override caretUpdate event to capture caret position changes
        public void caretUpdate(CaretEvent e) {
               
                /*
                 * To get the current position of caret use,
                 * int getDot()
                 * method of CaretEvent class.
                 */
               
                int position = e.getDot();
               
                //show current position in the status bar of an applet
               System.out.println(position);
        
          
      }          
public static void main(String args[]){
    
        
 new JTextFieldCaretPosition();
       
}
}
See also:

COMMENTS

Name

android arrays cnna1 graphics java java control statements java.swing JButton JFrame JLabel JTextField laravel node.js OPP questions swing technology
false
ltr
item
Code WHIZZ: How to Get Current Caret Position in JTextField
How to Get Current Caret Position in JTextField
Code WHIZZ
https://code-whizz.blogspot.com/2017/08/how-to-get-current-caret-position-in.html
https://code-whizz.blogspot.com/
http://code-whizz.blogspot.com/
http://code-whizz.blogspot.com/2017/08/how-to-get-current-caret-position-in.html
true
5534598864497432585
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy