Hopefully this code might help you in some way. You just have to do this
int len = textArea.getDocument().getLength();
textArea.setCaretPosition(len);
and for wraping the text, so that it scrolls down, as the length is more than the actual view use
textArea.setLineWrap(true);
Here is a sample Program for your understanding
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class CarotPosition extends JFrame
{
private JPanel panel;
private JTextArea textArea;
private JScrollPane scrollPane;
private JButton button;
public CarotPosition()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
panel = new JPanel();
panel.setLayout(new BorderLayout());
textArea = new JTextArea();
scrollPane = new JScrollPane(textArea);
textArea.setLineWrap(true);
button = new JButton("Click to add Text");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
textArea.append("Some NEW TEXT is here...");
int len = textArea.getDocument().getLength();
textArea.setCaretPosition(len);
textArea.requestFocusInWindow();
}
});
setContentPane(panel);
panel.add(scrollPane, BorderLayout.CENTER);
panel.add(button, BorderLayout.PAGE_END);
pack();
setVisible(true);
}
public static void main(String... args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new CarotPosition();
}
});
}
}
Hope this be of some help to you.
Regards