Scroll automático

Olá pessoal,

Meu problema é o seguinte. Tenho um JScrollPane e um JTextArea adicionado neste. O Scroll funciona perfeitamente. Mas eu queria que ele rolasse automaticamente à medida que a página (JTextArea) fosse aumentando o número de linhas.

É possível?
Obrigado.

Olha só,
geralmente isso de a scroll acompanhar, faz automatico…

tente setLineWrap(true);

Não sei bem o que faz isso hehehehe

Tchauzin!

Mas o scroll acompanha o texto, nao?
Olha esse exemplo


import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.Timer;

/**
 * @author fabiofalci
 */
public class AutoScroll extends JPanel implements ActionListener {

    private JTextArea textArea;
    private Random random = new Random(Calendar.getInstance().getTimeInMillis());
    
    public AutoScroll() {
        this.initialize();
        this.initThread();
    }
    
    protected void initialize() {
        this.setLayout(new BorderLayout());        
        this.textArea = new JTextArea();    
        this.add(new JScrollPane(this.textArea), BorderLayout.CENTER);                
    }
    
    public void initThread() {
        Timer t = new Timer(600, this);
        t.start();
    }

    public void actionPerformed(ActionEvent e) {
        byte[] b = new byte[this.random.nextInt(20)];
        this.random.nextBytes(b);
        
//        int position = this.textArea.getCaretPosition();
//        this.textArea.insert(new String(b) + "\n", position);
        this.textArea.append(new String(b) + "\n");
    }
    
    
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        AutoScroll auto = new AutoScroll();
        frame.setContentPane(auto);
        frame.setSize(300, 200);
        frame.setVisible(true);
    }
}

Seu exemplo depende que o cursor esteja posicionado ao final da JTextArea para funcionar. Ou seja, o autoscroll para se o usuário clicar em qualquer linha do texto.

Para resolve isso, você pode também modificar manualmente a posição do Scroll:

[code]
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.Timer;

/**

  • @author fabiofalci

  • @author ViniGodoy
    */
    public class AutoScroll extends JPanel implements ActionListener {

    private JTextArea textArea;
    private Random random = new Random(Calendar.getInstance().getTimeInMillis());
    private JScrollPane scrlText = null;

    public AutoScroll() {
    this.initialize();
    this.initThread();
    }

    protected void initialize() {
    this.setLayout(new BorderLayout());
    this.textArea = new JTextArea();
    this.textArea.setEditable(false);
    this.scrlText = new JScrollPane(this.textArea);
    this.add(scrlText, BorderLayout.CENTER);
    }

    public void initThread() {
    Timer t = new Timer(600, this);
    t.start();
    }

    public void actionPerformed(ActionEvent e) {
    byte[] b = new byte[this.random.nextInt(20)];
    this.random.nextBytes(b);
    this.textArea.append(new String(b) + “\n”);
    this.scrlText.getVerticalScrollBar().setValue(scrlText.getVerticalScrollBar().getMaximum());
    }

    public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    AutoScroll auto = new AutoScroll();
    frame.setContentPane(auto);
    frame.setSize(300, 200);
    frame.setVisible(true);
    }
    }[/code]

tente:

jTextArea1.setAutoscrolls(true);