Sign in to follow this  
Followers 0

2 posts in this topic

Posted

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;


		if(scrollBarVisible)

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


		recentChat.setText(Utils.buildRecentHistory(chatManager.getMessages()));


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

			recentChat.setCaretPosition(recentChat.getDocument().getLength());

		}

	}

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 ever-so-slightly 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:

2F0Ji.png

What it should look like:

8hTCV.png

Thanks for any help guys!

Share this post


Link to post
Share on other sites

Posted

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  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.