Java / Swing:JScrollPane中的JTextArea,如何防止自动滚动? [英] Java / Swing : JTextArea in a JScrollPane, how to prevent auto-scroll?
问题描述
这是一段可运行的代码,显示了我的问题。
here's a runnable piece of code that shows what my "problem" is.
我有一个 JTextArea
包装在 JScrollPane
中。当我更改 JTextArea
的文本时, JScrollPane
会自动滚动到文本的末尾而我不会希望如此。
I've got a JTextArea
wrapped in a JScrollPane
. When I change the text of the JTextArea
, the JScrollPane
scrolls automatically to the end of the text and I don't want that.
以下是我的要求:
- 应用程序不应该自动垂直滚动,但是......
- 用户应该能够垂直滚动
- 用户不应该能够水平滚动
- 应用程序永远不应水平滚动
- JTextArea必须是不可编辑的
- the application should not scroll vertically automatically but...
- the user should be able to scroll vertically
- the user should not be able to scroll horizontally
- the application should never scroll horizontally
- the JTextArea must not be editable
(所以即使文本比水平方式更多,应用程序和用户都不应该能够水平滚动。在垂直方向上,只有用户应该能够滚动。)
(so even if there's more text than what can fit horizontally, neither the application nor the user should be able to scroll horizontally. While vertically, only the user should be able to scroll.)
我不知道如何修复这个:这是否应该修复使用 JTextArea
或 JScrollPane
方法?
I don't know how to "fix" this: should this be fixed using JTextArea
or JScrollPane
methods?
请注意,AFAICT 不完全重复: JTextPane阻止在父级JScrollPane中滚动
Note that AFAICT this is not a duplicate at all of: JTextPane prevents scrolling in the parent JScrollPane
这是一个有趣的例子,它每隔200毫秒将新文本放入 JTextArea
,你可以看到 JScrollPane
总是自动滚动到文本末尾。
Here's the kinda funny example, every 200 ms it puts new text in the JTextArea
and you can see the JScrollPane
always scrolling automatically to the end of the text.
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public final class TextInScrollPane extends JFrame {
private static final Random r = new Random( 42 );
public static void main( final String[] args ) {
final JFrame f = new JFrame();
f.setDefaultCloseOperation( EXIT_ON_CLOSE );
f.setLayout(new BorderLayout());
final JTextArea jta = new JTextArea( "Some text", 30, 30 );
jta.setEditable( false ); // This must not be editable
final JScrollPane jsp = new JScrollPane( jta );
jsp.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
jsp.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
f.add( jsp, BorderLayout.CENTER );
f.pack();
f.setLocationRelativeTo( null );
f.setVisible(true);
final Thread t = new Thread( new Runnable() {
public void run() {
while ( true ) {
try {Thread.sleep( 200 );} catch ( InterruptedException e ) {}
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < 50 + r.nextInt( 75 ); i++) {
for (int j = 0; j < r.nextInt(120); j++) {
sb.append( (char) 'a' + r.nextInt(26) );
}
sb.append( '\n' );
}
SwingUtilities.invokeLater( new Runnable() {
public void run() {
jta.setText( sb.toString() );
}
} );
}
}
});
t.start();
}
}
推荐答案
http://download.oracle.com/javase/1.5.0/docs/api/javax/swing/text/DefaultCaret.html#NEVER_UPDATE
尝试:
JTextArea textArea = new JTextArea();
DefaultCaret caret = (DefaultCaret)textArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
这可以防止插入符号自动使文档滚动到底部。
This should prevent the caret from automatically making the document scroll to the bottom.
这篇关于Java / Swing:JScrollPane中的JTextArea,如何防止自动滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!