Question

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());
}
}
[/CODE]

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:

2F0Ji.png

What it should look like:

8hTCV.png

Thanks for any help guys!

Link to comment
https://www.neowin.net/forum/topic/1110783-making-jscrollbars-behave/
Share on other sites

1 answer to this question

Recommended Posts

This topic is now closed to further replies.
  • Posts

    • Vivaldi 8.0.4033.46 by Razvan Serea Vivaldi is a cross-platform web browser built for – and with – the web. A browser based on the Blink engine (same in Chrome and Chromium) that is fast, but also a browser that is rich in functionality, highly flexible and puts the user first. A browser that is made for you. Vivaldi is produced with love by a founding team of browser pioneers, including former CEO Jon Stephenson von Tetzchner, who co-founded and led Opera Software. Vivaldi’s interface is very customizable. Vivaldi combines simplicity and fashion to create a basic, highly customizable interface that provides everything a internet user could need. The browser allows users to customize the appearance of UI elements such as background color, overall theme, address bar and tab positioning, and start pages. Vivaldi features the ability to "stack" and "tile" tabs, annotate web pages, add notes to bookmarks and much more. Vivaldi 8.0.4033.46 fixes: [Chromium] Update to 148.0.7778.263 ESR (includes security fixes from 149.0.7827.102/103) Download: Vivaldi 64-bit | 139.0 MB (Freeware) Download: Vivaldi 32-bit | ARM64 View: Vivaldi Home Page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • I'm surprised they haven't found a way to fix that. As much as I don't like software wasting memory, a file manager is the kind of thing that makes sense to keep running in active memory for super-fast recall. I suspect that is why MS makes their File Explorer part of the main explorer.exe shell, so that it is guaranteed to always be running.
    • Seriously 1.8gig....so what. Any computer, if you are going to do serious work on, in 2026 should have a mimimum of 16gig and I would say 32-64gig is the way to go.
    • So again, you'd consider "I'm happy with my LG TV" to be a valid response to an article about PlayStation? I get the "I don't like any of their products" but how does that in any way invoke Linux? It just feels very random.
  • Recent Achievements

    • Week One Done
      FBSPL earned a badge
      Week One Done
    • One Year In
      Jim Dugan earned a badge
      One Year In
    • One Month Later
      Tommi118 earned a badge
      One Month Later
    • One Month Later
      sjbousquet earned a badge
      One Month Later
    • Week One Done
      sjbousquet earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      486
    2. 2
      PsYcHoKiLLa
      198
    3. 3
      +Edouard
      155
    4. 4
      Steven P.
      83
    5. 5
      ATLien_0
      69
  • Tell a friend

    Love Neowin? Tell a friend!