Sign in to follow this  

Making JScrollBars behave

Recommended Posts

Seizure1990    248

I have a JScrollPane and JTextPane that is to be used in a chat program. Therefore, I'd like standard chat window behavior where the scroll bar continues to automatically scroll down if (and ONLY if) the scroll bar was already at the bottom, and otherwise leaves it at the current value.

The only solution I've found through searching which comes close to working involves setting the caret position for the JTextPane, but that still has issues. First, the relevant code:

public synchronized void updateChat(){
JScrollBar scrollBar = chatView.getVerticalScrollBar();
boolean scrollBarVisible = scrollBar.isVisible();
boolean scrollBottom = false;

scrollBottom = scrollBar.getMaximum() == scrollBar.getValue() + scrollBar.getVisibleAmount();


if(!scrollBarVisible && scrollBar.isVisible() || scrollBottom) {

chatView is a JScrollPane. The problem with this method is that even though the caret is set to the maximum value, the scroll bar will actually just be [i]ever-so-slightly[/i] away from all the way down. Then, when another message comes in, it doesn't think it should continue to scroll down since it's only close to bottom, but not actually.

What it looks like after the caret reset:


What it should look like:


Thanks for any help guys!

Share this post

Link to post
Share on other sites
Seizure1990    248

Nobody here can help me wrangle the savage beast that is javax.swing? How sad! :'[

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.