Java Tips Weblog

  • Blog Stats

    • 2,569,183 hits
  • Categories

  • Archives

Text Area Scrolling

Posted by Rob Camick on October 22, 2008

A JTextArea, added to a scroll pane, is commonly used to display messages generated by an application as it is easy to add messages to the text area using the append(…) method. When using a text area in this manner there is usually also a requirement that the text area scroll as each new message is added. Sometimes the text area scrolls automatically and sometimes it doesn’t. Why is this?

Based on my observations, when using JDK1.4.2, I have noticed that two requirements must be met for automatic scrolling:

  • the append(…) must be done on the Event Dispatch Thread
  • the caret must currently be positioned at the end of the text area before the append(…) method is invoked

I have also noted that initializing a text area at creation time using any of the following approaches will cause the caret to be positioned at the start of the text area (not the end as might be expected), and therefore scrolling will not happen automatically:

  • JTextArea textArea = new JTextArea(“Initial text”, …);
  • textArea.setText(“Initial text”);
  • textArea.read(someFile, null);

So, in general, automatic scrolling will only work on an initially empty text area when you use the append(…) method that is invoked from the Event Dispatch Thread.

So what can we do when the above conditions are not met?

The most common suggestion you will find in the forums is to use code like the following:

textArea.append(...);
textArea.setCaretPosition(textArea.getDocument().getLength());

Unfortunately, you must reset the caret position with every append(…) to make sure the caret is at the end of the document.

Note: I probably should have titled this entry “Text Component Scrolling” since the suggestions will also apply to JTextPane.

Update September 29, 2015

When I originally wrote this blog I suggested you could use code like the following:

JTextArea textArea = new JTextArea();
DefaultCaret caret = (DefaultCaret)textArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

to cause the text area to scroll every time new text was added to the text area. This was wrong and was a misunderstanding of the “setUpdatePolicy(…)” functionality. There appears to be no API shortcut to force scrolling if any of the original 3 conditions are not met.

However, you can check out the Smart Scrolling link below which may provide a helpful scrolling feature.

See Also

Smart Scrolling

Related Reading

The Event Dispatch Thread
Java API: javax.swing.text.DefaultCaret.setUpdatePolicy(…)

14 Responses to “Text Area Scrolling”

  1. Himanshu Barve said

    Hi,
    this was really helpful. I need one more help.
    How can i keep no of rows constant in text area.
    I need to create a console window for my application.
    If rows exceeds predefined no of rows first rows must get disposed. As is first in first out after no of rows exceeds.

    Hoping for reply.
    Thanks ..
    Himanshu

    • Rob Camick said

      This is not a forum. I don’t answer general questions here, only questions about the code or concepts you find in the articles. However, if you search this blog then you should find something of interest.

  2. Nostrooo said

    Thanks for this information, it’s really helpful !

  3. Sungho Kim said

    Thanks for it.
    It’s a very useful information.

  4. albakhakh said

    Really thanks for the info!
    But I have another problem/goal that I wish you could have a solution for.
    I have a JTexArea to output things and a check box to disable and enable auto scrolling. I want to be able to change the behavior of JTextArea while it’s outputting simply by enabling and disabling the checkbox. Is there anyway to do that? Because your way only works at initialization.

    Thanks :)

  5. Rob Camick said

    You should be able to dynamically change the update policy of the scrollbar when the checkbox is clicked. If it doesn’t work as you expect then email your SSCCE using the “Contact Us” page and I’ll take ot a look to see if I can find any problem.

  6. Soumendra Dash said

    Actually I have used a text area in my code and my requirement is like whenever any text will append to it, the scrollbar will always show the bottom contents of the textarea.

    I used the below code.

    scrollpane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener()
    {
    public void adjustmentValueChanged(AdjustmentEvent ae)
    {
    ae.getAdjustable().setValue(ae.getAdjustable().getMaximum());
    }
    });

    But whenever I scroll it up to see the above contents of the text area, it forcefully coming down.
    That means the scrollbar is constant always in buttom most position.

    I need a solution for this..could you pls help me out regarding this..

  7. Mudhar said

    This works a treat ..ta

  8. JtheRocker said

    Hi Rob,

    Thanks for the great article. :)

    I have a simple question though, if you don’t find it out of the scope of this article. :)

    Is there any way I could fix the caret position to the beginning of the last row line rather than the end of the document which is done by caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

    Take for an example, I update my text area with a text as follows, which is repeated over and over again in the text area,
    The width of the text area is smaller than the text in the current line.

    |—- —–|
    This is a line that is displayed in the a JTextArea
    This is a line that is displayed in the a JTextArea
    …..
    …..
    …..
    This is a line that is displayed in the a JTextArea
    |—- —–|

    The problem is, the front part of the text in the current line gets out of scope of the text area, living behind the part that didn’t get out of the scope till the end of the current line.

    It would be like,

    |—- —–|
    …is displayed in the a JTextArea’
    …is displayed in the a JTextArea’
    …is displayed in the a JTextArea’
    …is displayed in the a JTextArea’
    |—- —–|

    You, can see the front part “This is a line that” getting out of scope of the width of the text area.

    Hope, I made myself clear.

    Thank you very much!

    • Rob Camick said

      If you are manually inserting the text, then you can manually position the caret where ever you want. Take a look at my Text Utilities article. Some methods there may help you or give you ideas. The gotoStartOfLine() method would be a good place to start.

  9. Pomoa said

    *6 years later*

    It seems to not work if the JTextArea is not editable, but it’s still very useful, thank you.

Leave a reply to Rob Camick Cancel reply