JTextComponent: addCaretListener(CaretListener listener)
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
public class MainClass {
public static void main(String args[]) throws Exception {
JFrame frame = new JFrame("Caret Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextArea textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
frame.add(scrollPane, BorderLayout.CENTER);
CaretListener listener = new CaretListener() {
public void caretUpdate(CaretEvent caretEvent) {
System.out.println("dot:"+ caretEvent.getDot());
System.out.println("mark"+caretEvent.getMark());
}
};
textArea.addCaretListener(listener);
frame.setSize(250, 150);
frame.setVisible(true);
}
}
Related examples in the same category