I am building a small Swing application that contains - amongst other things - a JButton
and a JTextArea
. When the user clicks the button, a bunch of text gets auto-generated inside the text area.
The amount of text that gets generated causes the text area to scroll down many, many rows. What I'd like to do is automatically generate the text, push it to the text area (something like myTextArea.setText(开发者_运维百科autoGeneratedText)
) and then reset the caret to the first line; basically to automatically scroll all the way back up to the top.
Is there any way to do this (auto-scroll up)? I've been pouring over the Java 6 docs and nothing obvious is cropping up; also no online searches revealed anything related.
Thanks in advance!
You just have to call myTextArea.setCaretPosition(0)
.
setCaretPosition()
精彩评论