Scrolling JScrollPane on last added row

I have the text JTextArea and JScrollPane pane = new JScrollPane (text), I put pane.setAutoScrolls (true). How to get this when I add text to the component text that scrolls the panel at the end (last line)?

+1
source share
3 answers

follows the link from this thread ScrollPane scrolls to the bottom problem

+3
source

The best (and as far as I can tell, as far as possible) explanation of how carriages move by Rob Kamik:

http://tips4java.wordpress.com/2008/10/22/text-area-scrolling/

+3
source

, EDT? append EDT, JTextArea .

, , :

import java.awt.TextArea;

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


public class Sample {

    public static void main(String[] args) {

        /*
         * Not on EDT
         */
        showAndFillTextArea("Not on EDT", 0, 0);

        /*
         * On EDT
         */
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                showAndFillTextArea("On EDT", 400, 0);
            }
        });
    }

    private static void showAndFillTextArea(String title, int x, int y) {

        JFrame frame = new JFrame(title);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JTextArea textArea = new JTextArea(20, 20);
        JScrollPane scrollPane = new JScrollPane(textArea);
        frame.getContentPane().add(scrollPane);
        frame.pack();
        frame.setLocation(x, y);
        frame.setVisible(true);
        for(int i = 0; i < 50; i++) {
            textArea.append("Line" + i + "\n");
        }
    }

}
+1
source

Source: https://habr.com/ru/post/1782255/


All Articles