Java Swing CaretListener handle text component caret event

Description

Java Swing CaretListener handle text component caret event

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

public class Main {

   public static void main(String[] a) {
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      JTextField textField = new JTextField();

      textField.addCaretListener(new CaretListener() {
         public void caretUpdate(CaretEvent e) {
            System.out.println(e);
         }// www .j  a  va2 s .  co  m
      });

      frame.add(new JScrollPane(textField));

      frame.setSize(300, 200);
      frame.setVisible(true);
   }

}



PreviousNext

Related