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 737911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:46:34+00:00 2026-05-14T07:46:34+00:00

I am attempting to mimic the functionality of Adium and most other chat clients

  • 0

I am attempting to mimic the functionality of Adium and most other chat clients I’ve seen, wherein the scrollbars advance to the bottom when new messages come in, but only if you’re already there. In other words, if you’ve scrolled a few lines up and are reading, when a new message comes in it won’t jump your position to the bottom of the screen; that would be annoying. But if you’re scrolled to the bottom, the program rightly assumes that you want to see the most recent messages at all times, and so auto-scrolls accordingly.

I have had a bear of a time trying to mimic this; the platform seems to fight this behavior at all costs. The best I can do is as follows:

In constructor:

JTextArea chatArea = new JTextArea();
JScrollPane chatAreaScrollPane = new JScrollPane(chatArea);

// We will manually handle advancing chat window
DefaultCaret caret = (DefaultCaret) chatArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);

In method that handles new text coming in:

boolean atBottom = isViewAtBottom();

// Append the text using styles etc to the chatArea

if (atBottom) {
    scrollViewportToBottom();
} 


public boolean isAtBottom() {
    // Is the last line of text the last line of text visible?
    Adjustable sb = chatAreaScrollPane.getVerticalScrollBar();

    int val = sb.getValue();
    int lowest = val + sb.getVisibleAmount();
    int maxVal = sb.getMaximum();

    boolean atBottom = maxVal == lowest;
    return atBottom;
}


private void scrollToBottom() {
    chatArea.setCaretPosition(chatArea.getDocument().getLength());
}

Now, this works, but it’s janky and not ideal for two reasons.

  1. By setting the caret position, whatever selection the user may have in the chat area is erased. I can imagine this would be very irritating if he’s attempting to copy/paste.
  2. Since the advancement of the scroll pane occurs after the text is inserted, there is a split second where the scrollbar is in the wrong position, and then it visually jumps towards the end. This is not ideal.

Before you ask, yes I’ve read this blog post on Text Area Scrolling, but the default scroll to bottom behavior is not what I want.

Other related (but to my mind, not completely helpful in this regard) questions:
Setting scroll bar on a jscrollpane
Making a JScrollPane automatically scroll all the way down.

Any help in this regard would be very much appreciated.

Edit:

As per Devon_C_Miller’s advice, I have an improved way of scrolling to the bottom, solving issue #1.

private void scrollToBottom() {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
       public void run() {
           try {
               int endPosition = chatArea.getDocument().getLength();
               Rectangle bottom = chatArea.modelToView(endPosition);
               chatArea.scrollRectToVisible(bottom);
           }
           catch (BadLocationException e) {
               System.err.println("Could not scroll to " + e);
           }
       }
    });
}

I still have problem #2.

  • 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-05-14T07:46:35+00:00Added an answer on May 14, 2026 at 7:46 am

    Take a look at the scrollRectToVisible(Rectangle r) and modelToView(int pos)

    That should get you what you’re looking for without disturbing the user’s selection.

    As for the scrollbar, try forcing the scroll and the append into occur on different events. For example:

    if (atBottom) {
        // append new line & do scroll
        SwingUtilities.invokerLater(new Runnable(){
            public void run() {
                // append text
            }});
    } else {
        // append newline
        // append text
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Attempting to get Spring internationalization working. I have used classpath:messages basename, created .properties files
Attempting to use XStream's JavaBeanConverter and running into an issue. Most likely I'm missng
I am attempting to mimic some stuff from Orchard CMS in my own app
I'm attempting to mimic (or find a preexisting component) that mimics the zoom slider
I am attempting to create a UIView (and associated UIViewController ) which mimic the
Attempting to learn Ruby using the new LearnStreet online tutorials. Have attempted to get
I'm attempting to mimic, as close as possible, the Heroku deployment environment but in
I am attempting to mimic the bing video search preview feature. I have a
Attempting the most basic example of Knockout.js possible on their documentation page: http://knockoutjs.com/documentation/observables.html Looks
Attempting to build websocket++ for Windows. g++ -Wall -O2 -I/c/boost/boost_1_52_0//boost chat_server.o chat.o -o chat_server

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.