Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JViewport;

public class Main {

    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);
        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());
    }
}