JTextComponent: addCaretListener(CaretListener listener) : JTextComponent « javax.swing « Java by API






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

1.JTextComponent.KeyBinding
2.JTextComponent: getActions()
3.JTextComponent: getCaretPosition()
4.JTextComponent: getDocument()
5.JTextComponent: getHighlighter()
6.JTextComponent: getSelectedText()
7.JTextComponent: getText(int offs, int len)
8.JTextComponent: moveCaretPosition(int pos)
9.JTextComponent: print(MessageFormat headerFormat, MessageFormat footerFormat, boolean showPrintDialog, PrintService service, PrintRequestAttributeSet attributes, boolean interactive)
10.JTextComponent: read(Reader in, Object desc)
11.JTextComponent: replaceSelection(String content)
12.JTextComponent: select(int selectionStart, int selectionEnd)
13.JTextComponent: setDocument(Document doc)
14.JTextComponent: setDragEnabled(boolean b)
15.JTextComponent: setFocusAccelerator(char aKey)
16.JTextComponent: setNavigationFilter(NavigationFilter filter)
17.JTextComponent: setSelectedTextColor(Color c)
18.JTextComponent: setSelectionColor(Color c)
19.JTextComponent: setSelectionEnd(int selectionEnd)
20.JTextComponent: setSelectionStart(int selectionStart)
21.JTextComponent: write(Writer out)