如何滚动GWT TextArea? [英] How to scroll a GWT TextArea?
问题描述
我正在调用 com.google.gwt.user.client.ui.TextArea.setText(myText)
来设置内容。之后,我调用 setCursorPosition(myText.length())
来将光标移动到最后。这很好。
I am calling com.google.gwt.user.client.ui.TextArea.setText(myText)
to set the content. After that I call setCursorPosition(myText.length())
to get the cursor to the end. This works well.
当 myText
有更多的行时,文本区域可以立即显示,它显示滚动酒吧。但它不滚动到光标位置。更糟的是,它滚动到顶端。
When myText
has more lines then the text area can display at once, it shows a scroll bar. But it does not scroll to the cursor position. Even worse - it scrolls to the top.
如何将GWT TextArea滚动到光标位置?我真的需要光标位置,而不是TextArea的底部。 JSNI解决方法也可以。
How can I scroll the GWT TextArea to the cursor position? I realy need the cursor position, not the bottom of the TextArea. A JSNI workaround would be ok too.
推荐答案
我有一个场景,textarea已经有了,当一个新的命令是提交它会将数据添加到它并滚动到新添加的数据的开始。这就是我所做的
I had a scenario where the textarea will already have something and when a new command is submitted it will add the data to it and scroll to the start of the newly added data. this is what I did
// Hold the previous height to set the scroll.
final int prevHeight = document.get().getElementById(textareadid).getScrollHeight();
// Hold the prev position if output area already has some data.
final int prevPos = this.textArea.getValue() != null ?
this.textArea.getValue().length() : 0;
处理并设定新资料后
int posCount = 0;
if (previousResponse != null && !previousResponse.isEmpty())
{
final String dataStr = "new data from process";
// add 15 lines for the cursor position
posCount = getRelativeCount(dataStr);
}
this.textArea.getElement().setScrollTop(prevHeight);
this.textArea.setCursorPos(prevPos + posCount);
private int getRelativeCount(final String str)
{
int charCount = 0;
if (str != null)
{
int NUM_ROWS = 15;
if (getUserAgent().contains("msie"))
{
NUM_ROWS = 16;
}
final String[] splitArr = str.split("\n"); // split on the new line
// char
for (int index = 0; index < splitArr.length && index < NUM_ROWS; index++)
{
charCount += splitArr[index].length();
}
}
return charCount;
}
这篇关于如何滚动GWT TextArea?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!