title: "Smart" autoscrolling in Java?
body: <p>NOTE: I am reasking <a href="https://stackoverflow.com/questions/3392290/smart-jscrollpane-autoscrolling">this</a> question because I need an answer to it, and the answer there has a broken link.</p>
  
  <p>I am writing a simple chat program as part of a project for school, and I wanted the user to be able to automatically track messages as they come if they are at the bottom, but not be bothered by having to scroll up again if they are looking at something higher up. Currently, my code is just scroll to the bottom no matter what. Basic code is below, and I really don't know how to convert it to an SSCCE. If anyone can, please feel free to do so.</p>
  
  <pre><code>JTextArea jta = new JTextArea(50, 50);
  JScrollPane scroll = new JScrollPane(jta);
  try {
      int iter = 0;
      while (true) { //Not a for loop because it should not end after x iterations
          Thread.sleep(500);
          jta.append("Test text #" + iter);
          iter++;
          //Check if they are at the bottom and if so scroll down to the new bottom.
      }
  } catch (InterruptedException ex) {
      System.out.println("Error!");
  }
  </code></pre>