title: Swing: Scroll to bottom of JScrollPane, conditional on current viewport location
body: <p>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.</p>
  
  <p>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:</p>
  
  <p>In constructor:</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>In method that handles new text coming in:</p>
  
  <pre><code>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());
  }
  </code></pre>
  
  <p>Now, this works, but it's janky and not ideal for two reasons.</p>
  
  <ol>
  <li>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.</li>
  <li>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. </li>
  </ol>
  
  <p>Before you ask, yes I've read this blog post on <a href="http://tips4java.wordpress.com/2008/10/22/text-area-scrolling/" rel="nofollow noreferrer">Text Area Scrolling</a>, but the default scroll to bottom behavior is not what I want.  </p>
  
  <p>Other related (but to my mind, not completely helpful in this regard) questions:
  <a href="https://stackoverflow.com/questions/1166072/setting-scroll-bar-on-a-jscrollpane">Setting scroll bar on a jscrollpane</a> 
  <a href="https://stackoverflow.com/questions/2483572/making-a-jscrollpane-automatically-scroll-all-the-way-down">Making a JScrollPane automatically scroll all the way down.</a></p>
  
  <p>Any help in this regard would be very much appreciated.</p>
  
  <p>Edit:</p>
  
  <p>As per Devon_C_Miller's advice, I have an improved way of scrolling to the bottom, solving issue #1.</p>
  
  <pre><code>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);
             }
         }
      });
  }
  </code></pre>
  
  <p>I still have problem #2.</p>