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

发布于 2024-08-09 10:05:47 字数 212 浏览 8 评论 0原文

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

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

我怎样才能做到这一点?

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

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

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.

How can I do that?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

相思碎 2024-08-16 10:05:47

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

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

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

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

注意:

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

相反,您可能想查看智能滚动,它为用户提供了能够确定何时应该自动滚动或不自动滚动。

有关文本区域中自动滚动的更详细说明,请参见:文本区域滚动

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

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:

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 Scrolling which gives the user the ability to determine when scrolling should be automatic or not.

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

江湖彼岸 2024-08-16 10:05:47
    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);
        }
    }
    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);
        }
    }
落墨 2024-08-16 10:05:47

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

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.

掌心的温暖 2024-08-16 10:05:47
    JTextArea jTextArea = new JTextArea();
    DefaultCaret caret = (DefaultCaret)jTextArea.getCaret();
    caret.setUpdatePolicy(DefaultCaret.OUT_BOTTOM);
    JTextArea jTextArea = new JTextArea();
    DefaultCaret caret = (DefaultCaret)jTextArea.getCaret();
    caret.setUpdatePolicy(DefaultCaret.OUT_BOTTOM);
掩饰不了的爱 2024-08-16 10:05:47

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

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

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

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

当文本包含由 UTF-16 编码中的两个字符(两个 16 位单元)表示的字符(所谓的代理对,如下所示:

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

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).

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.

一紙繁鸢 2024-08-16 10:05:47

最好和最简单的方法,试试这个:

  import javax.swing.text.DefaultCaret;
  DefaultCaret caret = (DefaultCaret) textArea.getCaret();
  caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

Best and easiest way, Try this :

  import javax.swing.text.DefaultCaret;
  DefaultCaret caret = (DefaultCaret) textArea.getCaret();
  caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
南汐寒笙箫 2024-08-16 10:05:47

用这个代替

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

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);
羅雙樹 2024-08-16 10:05:47

试试这个:

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

Try this:

JTextArea jTextArea = new JTextArea();
JScrollPane jScrollPane = new JScrollPane();
jScrollPane.setViewport(jTextArea);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文