1

I have already seen : How to set AUTO-SCROLLING of JTextArea in Java GUI?

    blackArea = new JTextArea();
    blackArea.setFont(new Font("Tahoma", Font.BOLD, 11));
    blackArea.setText("Loggend on Administrator...\n" +date);
    blackArea.setForeground(Color.RED);
    blackArea.setBackground(Color.BLACK);

    DefaultCaret caret = (DefaultCaret)blackArea.getCaret();
    caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
    blackArea.setCaretPosition(blackArea.getDocument().getLength());
    scrollPane.setViewportView(blackArea);

This works well. When update to JTextArea, the scroll moved to bottom automatically so I could see the refresh data. But the problem is, when I click the any space in JTextArea, the auto-scrolling is stopped. No more auto scroll works. How to fix it?

SUPPLEMENT : I added text to blackArea calling GUI.blackArea.append("bla bla bla"); GUI is class name where above code included. Thanks for @hovercraft-full-of-eels

Community
  • 1
  • 1
ton1
  • 7,238
  • 18
  • 71
  • 126

3 Answers3

4

Check out Smart Scrolling. It is an improvement over the other scrolling answer.

It the scrollpane is at the bottom when the append occurs it will continue to keep the scrollpane at the bottom. However, if the user has move the viewport from the bottom then the append will not automatically scroll to the bottom.

camickr
  • 321,443
  • 19
  • 166
  • 288
2

You don't show where you are adding or appending text to the JTextArea, and this is critical since the changing of the caret position should occur there.


Edit
You state:

Sorry, I just append text in other class, just calling GUI.blackArea.append("bla bla bla"); Should I use SwingUtilities.invokeLater?

I know you've got a decent answer from Rob Camick, a true Swing guru, but I also have to add that you really shouldn't expose your class's fields that way (and hopefully none of your components are declared static as your code suggests that they may be). Instead expose public methods that allow controlled ability to change the state of your fields. For instance your GUI class could have a public method like so

public void blackAreaAppend(String text) {
   blackArea.append(text);
   // code here to advance cursor
}

Or if this method is always called off of the EDT:

public void blackAreaAppend(String text) {
  SwingUtilities.invokeLater(new Runnable() {
    public void run() {
      blackArea.append(text);
      // code here to advance cursor
    }
  });
}

Or if you're just not sure:

public void blackAreaAppend(String text) {

  if (SwingUtilities.isEventDispatchThread()) {
    blackArea.append(text);
    // code here to advance cursor    
  } else {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        blackArea.append(text);
        // code here to advance cursor
      }
    });
  }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
0

I solved this problem. this is the problem of view-point. when I click any space on JTextarea, the location of caret is changed, so view-point is changed too. Following my code, there is no update with view point.

  • So, I made a method :

    public static void addLog(final String log){
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            GUI.blackArea.append(log);
            GUI.blackArea.setCaretPosition(GUI.blackArea.getDocument().getLength());
        }
    });
    

    }

I changed blackArea.append("...") to `addLog("...). and I got out of this problem, However, remember that you can't fix caret positon while updating.

ton1
  • 7,238
  • 18
  • 71
  • 126