Example usage for javax.swing JTextArea addCaretListener

List of usage examples for javax.swing JTextArea addCaretListener

Introduction

In this page you can find the example usage for javax.swing JTextArea addCaretListener.

Prototype

public void addCaretListener(CaretListener listener) 

Source Link

Document

Adds a caret listener for notification of any changes to the caret.

Usage

From source file:Main.java

public static void main(String[] args) {
    String text = "one\ntwo\nthree\nfour\nfive";
    JFrame frame = new JFrame("title");
    JTextArea textArea = new JTextArea(text, 1, 30); // shows only one line
    JScrollPane scrollPane = new JScrollPane(textArea);
    frame.add(scrollPane);// ww w .  ja  va  2s.c om
    frame.pack();
    frame.setVisible(true);

    final JViewport viewport = scrollPane.getViewport();

    textArea.addCaretListener(e -> {
        System.out.println("First : " + viewport.getViewPosition());
        System.out.println("Second: " + viewport.getViewPosition());
    });
    textArea.setCaretPosition(text.length());
}

From source file:MainClass.java

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());
        }/*from   ww w .j  av  a  2 s .c  o m*/
    };

    textArea.addCaretListener(listener);

    frame.setSize(250, 150);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    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());
        }/*  w  w w  . j  a  v a  2s.c o m*/
    };

    textArea.addCaretListener(listener);

    frame.setSize(250, 150);
    frame.setVisible(true);
}

From source file:CaretSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Caret Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = frame.getContentPane();
    JTextArea textArea = new JTextArea();
    JScrollPane scrollPane = new JScrollPane(textArea);
    content.add(scrollPane, BorderLayout.CENTER);

    final JTextField dot = new JTextField();
    dot.setEditable(false);//w w  w.  j  ava  2  s.  c  o  m
    JPanel dotPanel = new JPanel(new BorderLayout());
    dotPanel.add(new JLabel("Dot: "), BorderLayout.WEST);
    dotPanel.add(dot, BorderLayout.CENTER);
    content.add(dotPanel, BorderLayout.NORTH);

    final JTextField mark = new JTextField();
    mark.setEditable(false);
    JPanel markPanel = new JPanel(new BorderLayout());
    markPanel.add(new JLabel("Mark: "), BorderLayout.WEST);
    markPanel.add(mark, BorderLayout.CENTER);
    content.add(markPanel, BorderLayout.SOUTH);

    CaretListener listener = new CaretListener() {
        public void caretUpdate(CaretEvent caretEvent) {
            dot.setText("" + caretEvent.getDot());
            mark.setText("" + caretEvent.getMark());
        }
    };

    textArea.addCaretListener(listener);

    frame.setSize(250, 150);
    frame.setVisible(true);
}