1

Possible Duplicate:
Automatically scroll to the bottom of a text area

I have TextArea component. In different situation i should append text to it.I want Caret to be appears at the end of new appended text and if text is to large, automatically scrolling down.

textAreaStatus = new WebTextArea(
            "1- Click on the refresh icon to get newest file.\n" +
                    "2- Select destination if needed.\n" +
                    "3- Click download button to start downloading.\n");
    textAreaStatus.setBackground(Color.black);
    textAreaStatus.setCaretPosition(textAreaStatus.getText().length());
    textAreaStatus.getCaret().setVisible(true);
Community
  • 1
  • 1
itro
  • 7,006
  • 27
  • 78
  • 121
  • 1
    So what's the question? What happens when you execute your code? – Aleks G Jan 26 '12 at 15:31
  • stick to your first and read the linked article given there - then describe exactly what you don't understand ... – kleopatra Jan 26 '12 at 15:52
  • Add `textAreaStatus` component inside a `JScrollPane` container and set the latter with `setAutoscrolls(true)` – ecle Jan 26 '12 at 15:58
  • @eee no, that's completely wrong: autoscrolls has nothing to do with caret movements, especially not when setting it on the scrollPane ... – kleopatra Jan 26 '12 at 16:08
  • @kleopatra Yes, I agree but I was referring to "...automatically scrolling down" which autoscrolls method can do the job upon the component it contains. – ecle Jan 26 '12 at 16:31
  • @kleopatra Hmm, it looks like it doesn't automatically scrolling down anyway unless it is dragged outside..a confusing method name – ecle Jan 26 '12 at 16:42
  • Wow this question is wonderfully explained in that previous thread. @itro, you must understand basics, sometimes the code speaks for itself, "Where I am to be used..." :-) Regards – nIcE cOw Jan 26 '12 at 16:44

1 Answers1

8

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

nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
  • It's not necessary to add `textArea.requestFocusInWindow()` if the only point is to SHOW the bottom of the text area to the user. – DSlomer64 Mar 29 '15 at 18:17
  • @DSlomer64: The line pointed to, is merely to keep the focus on that respective `JTextField`, since, while writing this snippet, I might have used additional `JTextField`, so to keep focus on this one, I might have used that. It has nothing to do with bringing the cursor down – nIcE cOw Mar 30 '15 at 15:29