2010-03-09 62 views
4

我打電話給com.google.gwt.user.client.ui.TextArea.setText(myText)來設置內容。之後,我撥打setCursorPosition(myText.length())將光標移到最後。這很好。如何滾動GWT TextArea?

myText有更多的行,那麼文本區域可以立即顯示,它會顯示一個滾動條。但它不滾動到光標位置。更糟 - 它滾動到頂部。

如何將GWT TextArea滾動到光標位置?我真的需要光標位置,而不是TextArea的底部。 JSNI解決方法也可以。

回答

3

我有一個場景,textarea已經有了一些東西,當一個新的命令被提交時,它會將數據添加到它並滾動到新添加的數據的開始。這是我做過什麼

// 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; 
} 
3

嘗試設置光標位置之後加入此:

textAreaToScroll.getElement().setScrollTop(textAreaToScroll.getElement().getScrollHeight()); 

這將在元件滾動到底部。

編輯:

要滾動至任何光標位置存在(據我所知)沒有簡單的方法來做到這一點。我不認爲有什麼辦法可以問光標所在的瀏覽器。 我剛剛想到了一些可能有用的事情(還沒有真正測試過)來猜測滾動需要多長時間的粗略估計。

int cursorPos = textAreaToScroll.getCursorPos(); 
long offsetRatio = cursorPos/textAreaToScroll.getText().length(); 
//gives 0.0 to 1.0 
offsetRatio += someMagicNumber; // -0.1 maybe? 
// Depending on the font you may need to adjust the magic number 
// to adjust the ratio if it scrolls to far or to short. 
offsetRatio = offsetRatio>0.0 ? offsetRatio : 0; //make sure 
//we don't get negative ratios 
//(negative values may crash some browsers while others ignore it) 
textAreaToScroll.getElement().setScrollTop(
    textAreaToScroll.getElement().getScrollHeight() * offsetRatio); 

這可以滾動大致所需的距離。請注意,這假定每行都被填充大約相同的數量,因爲它使用光標位置除以文本的長度而不是行數(這很難計算)。手動換行會扭曲這一估計,而比例字體也會使其不太準確。

您可能需要調整比率,以使其滾動得太短而不是太遠,因爲如果光標稍微低於文本區域的頂部,光標仍然可見。正如我所說我沒有真正測試過這一點,我可能已經顛倒了邏輯和其他微妙的錯誤。

+0

此代碼將TextArea滾動到底部,不幸的是不會移動到光標位置:-( – Witek 2010-03-11 11:11:39

+0

如果將光標設置到最後,它不會在底部? – 2010-03-12 10:53:35

+0

我需要一個解決方案,其中TextArea滾動到光標的位置,而不是結束。示例:TextArea有5行高度。其文字內容爲50行。光標位於第25行。我想要一個方法scrollToCursorPosition(),它可以滾動到第25行,而不是第50行。 – Witek 2010-03-19 09:28:45

1

爲了改善斯坦的答案,你可以指望在文本行數,然後基於頂部位置在總行的所需行上,而不是使用字符。

當你計算行,你還必須確定哪些行的光標是在

String text = textArea.getText(); 
int lines = 1; 
int pos = 0; 
int cursorPos = ...; 
int cursorLine = -1; 
while((pos = 1+text.indexOf("\n", pos)) > 0) 
{ 
    if (cursorLine == -1 && pos > cursorPos) 
     cursorLine = lines; 
    lines++; 
} 
if (lines > 0 && cursorLine > 0 && cursorLine < lines) 
{ 
    int scroll = textArea.getElement().getScrollHeight(); 
    scroll *= cursorLine; 
    scroll /= lines; 
    scroll -= 30; // Back up a bit so it's not right at the top 
    if (scroll < 0) 
     scroll = 0; 
    textArea.getElement().setScrollTop(scroll); 
}