问题
I am trying to implement a JScrollPane with a JTextArea. The JTextArea is being appended to, and I want the JScrollPane to keep scrolling down as more text is added. How can this be achieved?
回答1:
For (what I think is) a simpler answer check out: Text Area Scrolling.
Prior to JDK5, you would have to manually change the caret's position after each append. You can now give this behaviour as a default like this :
JTextArea textArea = new JTextArea(); DefaultCaret caret = (DefaultCaret)textArea.getCaret(); caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
The advantage of this is that you don't need to use this snippet more than once in your code!
回答2:
I found the answer here: JScrollPane and JList auto scroll
scrollPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
e.getAdjustable().setValue(e.getAdjustable().getMaximum());
}
});
回答3:
If you are constantly writing data to it you could use:
textArea.setCaretPosition(textArea.getDocument().getLength());
just after you add the new data.
This would automatically scroll all the way down the JScorllPane.
回答4:
Here is the solution.
JTextArea textArea = new JTextArea();
DefaultCaret caret = (DefaultCaret)textArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);`
回答5:
A work around is possible: you can declare that listener as a class then instantiate it on the event where it is needed. After which you can remove the class after forcing a repaint of the screen. Works like a charm.
回答6:
My solution

import javax.swing.*;
import javax.swing.text.DefaultCaret;
public class NewClass {
public static void main(String[] args) {
JFrame win = new JFrame("test");
win.setSize(800, 500);
win.setResizable(false);
win.setLayout(null);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setLocationRelativeTo(null);
JTextArea txtListNum = new JTextArea();
JScrollPane barList = new JScrollPane(txtListNum);
DefaultCaret crList = (DefaultCaret) txtListNum.getCaret();
crList.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
barList.setLocation(10, 10); // Scrollbar Position
barList.setSize(180, 450); // Scrollbar Dimession
win.add(barList); // Scrolbar add to Frame
win.setVisible(true);
}
}
回答7:
The accepted solution works good, but only when the text area is editable, i.e. without jTextArea.setEditable(false)
. The solution suggested by Krigath is more general, but has the problem as asked here JScrollPane and JList auto scroll. Using answers from that question you can get general solution, e.g.:
JScrollPane scrollPane = new JScrollPane(jTextArea);
verticalScrollBarMaximumValue = scrollPane.getVerticalScrollBar().getMaximum();
scrollPane.getVerticalScrollBar().addAdjustmentListener(
e -> {
if ((verticalScrollBarMaximumValue - e.getAdjustable().getMaximum()) == 0)
return;
e.getAdjustable().setValue(e.getAdjustable().getMaximum());
verticalScrollBarMaximumValue = scrollPane.getVerticalScrollBar().getMaximum();
});
The Pane then is scrolled down only when vertical scroll bar is expanding, in response to appended lines of text.
I admit that that a method to filter events without extra variables could be found, and would appreciate if somebody post it.
来源:https://stackoverflow.com/questions/2483572/making-a-jscrollpane-automatically-scroll-all-the-way-down