Java Swing:在文本区域动态附加文本的方法,具有滚动条更新 [英] Java Swing: Approach for dynamically appending text in text area, have scrollbar update

查看:46
本文介绍了Java Swing:在文本区域动态附加文本的方法,具有滚动条更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java Swing 更新带有文本行(例如来自线程)的文本区域的一般方法是什么,然后在添加文本时让文本插入符号流到文本区域的底部.同时更新滚动条,使其位于底部.

What is the general approach with Java swing to update a textarea with lines of text (say from a Thread) and then have the text caret flow to the bottom of the textarea as text is being added. Also update the scrollbar so that it is at the bottom.

我想我会有一个字符串缓冲区并在其中附加文本,然后在 textarea 中设置字符串并将滚动条定位在底部.

I was thinking that I would have a stringbuffer and append text to that and then set the string in the textarea and position the scrollbar at the bottom.

推荐答案

使用 append() 添加文本,然后 setCaretPosition() 以确保滚动.

Use append() to add the text, then setCaretPosition() to make sure you scroll with it.

myTextPane.append(textFromSomewhere);
myTextPane.setCaretPosition(myTextPane.getDocument().getLength());

这篇关于Java Swing:在文本区域动态附加文本的方法,具有滚动条更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆