如何在 Java GUI 中设置 JTextArea 的自动滚动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1627028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:59:46  来源:igfitidea点击:

How to set AUTO-SCROLLING of JTextArea in Java GUI?

javauser-interfaceswingjscrollpanejtextarea

提问by Yatendra Goel

I have embedded a JTextArea on a JScrollPane and am using that JTextArea for output.

我在 JScrollPane 上嵌入了 JTextArea 并使用该 JTextArea 进行输出。

I want that whenever the ouput goes beyond the size of the JTextArea, the JTextArea scrolls automatically so that user don't have to do manual scroll down to see the recent output.

我希望每当输出超出 JTextArea 的大小时,JTextArea 会自动滚动,这样用户就不必手动向下滚动来查看最近的输出。

How can I do that?

我怎样才能做到这一点?

I have already set the autoscroll property of both JTextArea and JScrollPane to true.

我已经将 JTextArea 和 JScrollPane 的 autoscroll 属性设置为 true。

采纳答案by camickr

When using JDK1.4.2 (or earlier) the most common suggestion you will find in the forums is to use code like the following:

使用 JDK1.4.2(或更早版本)时,论坛中最常见的建议是使用如下代码:

textArea.append(...);
textArea.setCaretPosition(textArea.getDocument().getLength());

However, I have just noticed that in JDK5 this issue has actually been resolved by an API change. You can now control this behaviour by setting a property on the DefaultCaret of the text area. Using this approach the code would be:

但是,我刚刚注意到在 JDK5 中这个问题实际上已经通过 API 更改解决了。您现在可以通过在文本区域的 DefaultCaret 上设置一个属性来控制此行为。使用这种方法,代码将是:

JTextArea textArea = new JTextArea();
DefaultCaret caret = (DefaultCaret)textArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

Note:

笔记:

The above suggestion to set the caret update policy does not work.

上述设置插入符号更新策略的建议不起作用。

Instead you may want to check out Smart Scrollingwhich gives the user the ability to determine when scrolling should be automatic or not.

相反,您可能想要查看Smart Scrolling,它使用户能够确定何时应该自动滚动。

A more detailed description of automatic scrolling in a text area can be found here: Text Area Scrolling

可以在此处找到有关文本区域中自动滚动的更详细说明:文本区域滚动

回答by user211233

    JScrollBar vbar = scrollPane.getVerticalScrollBar();

    for (int i = 0; i < 20; i++) {

        myJTxt.append("This is text " + i + "\n");
        vbar.setValue(vbar.getMaximum());
        vbar.paint(vbar.getGraphics());
        myJTxt.scrollRectToVisible(myJTxt.getVisibleRect());
        myJTxt.paint(myJTxt.getGraphics());
        try {
            Thread.sleep(250);
        } catch (InterruptedException ex) {
            Logger.getLogger(ScrollTextView.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

回答by Honest

Try this:

尝试这个:

JTextArea jTextArea = new JTextArea();
JScrollPane jScrollPane = new JScrollPane();
jScrollPane.setViewport(jTextArea);

回答by Juntae

When you click anywhere over JTextArea, auto-scrolling have possible to be stopped. Because the position of caret once changed, view point changed too. In this time you should set caret position when you append or add some text. On my way, I made method including set caret position, and then use it when anything to be added or appended.

当您单击 JTextArea 上的任意位置时,可能会停止自动滚动。因为插入符号的位置一旦改变,视角也随之改变。这时候你应该在附加或添加一些文本时设置插入符号位置。在我的路上,我制作了包括设置插入符号位置的方法,然后在要添加或附加任何内容时使用它。

回答by kayz1

    JTextArea jTextArea = new JTextArea();
    DefaultCaret caret = (DefaultCaret)jTextArea.getCaret();
    caret.setUpdatePolicy(DefaultCaret.OUT_BOTTOM);

回答by JavaBeginner

Use this instead

改用这个

JTextArea textArea = new JTextArea();
DefaultCaret caret = (DefaultCaret)textArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
scrollPane = new JScrollPane();
scrollPane.add(textArea);
scrollPane.setViewportView(textArea);

回答by HAig

I tried most suggestions but ran into problems when the content of the JTextArea becomes large (several MB). Finally the following showed best performance:

我尝试了大多数建议,但是当 JTextArea 的内容变大(几 MB)时遇到了问题。最后,以下显示了最佳性能:

myTextArea.append( someText );
myTextArea.getCaret().setDot( Integer.MAX_VALUE );

Of course, any selection the user had made gets lost. Hence, its only usable for a "display-only" usage of the text area.

当然,用户所做的任何选择都会丢失。因此,它仅可用于文本区域的“仅显示”用途。

Still, in my installation, if the content of the JTextArea exceeds some 9MB, it gets kind of unusable (very sluggish to almost frozen GUI).

尽管如此,在我的安装中,如果 JTextArea 的内容超过 9MB,它就会变得无法使用(非常缓慢,几乎冻结了 GUI)。

A similar phenomenon occurs when the text contains characters that are represented by two chars (two 16-bit units) in the UTF-16 encoding (so-called surrogate pairs, like this: ). I have a solution for filtering, but maybe a different topic.

当文本包含以 UTF-16 编码(所谓的代理对,如下所示: )中的两个字符(两个 16 位单元)表示的字符时,会发生类似的现象。我有一个过滤解决方案,但可能是一个不同的主题。