Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7965087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:03:32+00:00 2026-06-04T06:03:32+00:00

I have a simple Java question here. I want to auto text scroll to

  • 0

I have a simple Java question here. I want to auto text scroll to the beginning of the last line of a text area created using JTextArea. The amount of text per line of the text area is quite longer than the width of the text area.

Here is the code snippet I used to set that up.

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

The problem now is, with the above code, the default behavior is that the caret is auto positioned to the end of the document, as a result, the beginning part of the whole text area gets out of scope. I’d prefer the auto scroll to happen to the beginning of the last line in the document.

To make it clear, here are two screen shots,

What I want is the first one but what’s happening is the second one.

What I want is this
What I get is this

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T06:03:33+00:00Added an answer on June 4, 2026 at 6:03 am

    Simply move the caret to the correct location using getLineCount and getLineStartOffset after updating the text of the textarea.

    Here is a working example illustrating your desired behaviour:

    import java.util.List;
    
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.SwingUtilities;
    import javax.swing.SwingWorker;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.DefaultCaret;
    
    public class Test {
    
        private JFrame frame;
        private JTextArea ta;
    
        protected void initUI() {
            frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            ta = new JTextArea();
            DefaultCaret caret = (DefaultCaret) ta.getCaret();
            caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
            frame.add(new JScrollPane(ta));
            frame.setSize(400, 200);
            frame.setVisible(true);
            new UpdateText().execute();
        }
    
        class UpdateText extends SwingWorker<String, String> {
    
            @Override
            public String doInBackground() {
                for (int i = 0; i < 1000; i++) {
                    publish("Hello-" + i);
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                return null;
            }
    
            @Override
            public void process(List<String> chunks) {
                for (String s : chunks) {
                    if (ta.getDocument().getLength() > 0) {
                        ta.append("\n");
                    }
                    ta.append(s);
                }
                try {
                    ta.setCaretPosition(ta.getLineStartOffset(ta.getLineCount() - 1));
                } catch (BadLocationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
    
            @Override
            public void done() {
    
            }
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new Test().initUI();
                }
            });
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question regarding the Java Memory Model. Here is a simple class
i found many related questions here. I have a simple java program . it
Simple question. I have a new list and an old list. In Java is
I have a simple java project (adapted from the example here ), which is
In my program I want to draw a simple score line graph. I have
I want to call a C subroutine from Java. I'm using JNI. I have
I have a simple java web service. I want to have the web service
I have Java-related question: I want to know is there a way to create
I have Simple java program named MainController.java. Wehn I try to compile it from
I have a simple Java class that I need to serialize to be stored

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.