How to scroll through GWT TextArea?

I call 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. It works well.

When myText has more lines, the text area can be displayed immediately, it shows the scroll bar. But it does not scroll to the cursor position. Even worse, it scrolls up.

How to scroll GWT TextArea to cursor position? I really need a cursor position, not the bottom of a TextArea. The JSNI workaround will be fine too.

+4
source share
4 answers

I had a scenario in which textarea already has something, and when a new command is sent, it will add data to it and scroll to the beginning 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; 

after processing and installing new data

  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
source

Try adding this after setting the cursor position:

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

This will scroll the item to the bottom.

EDIT:

There is no easy way to do this to scroll to any cursor position (as far as I know). I don’t think there is any way to ask the browser on which the cursor is on. I just got an idea of ​​something that might work (I didn't actually test it) to guess a rough estimate of how much time is scrolling.

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

This can scroll approximately the desired distance. Please note: this assumes that each line is filled with approximately the same amount, since it uses the cursor position divided by the length of the text, and not the number of lines (which are difficult to calculate). Manual newline characters will distort this estimate, and proportional fonts will also make it less accurate.

You will probably need to adjust the ratio so that it scrolls too short and not too far, since the cursor will still be visible if it is slightly below the top of the text area.

As I said, I did not actually test this, I may have turned the logic and other subtle errors.

+3
source

To improve Stein's answer, you can count the number of lines in the text, and then set the top position based on the desired line on common lines, rather than using characters.

While you are counting the lines, you also need to determine which line the cursor is on.

 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 not right at the top if (scroll < 0) scroll = 0; textArea.getElement().setScrollTop(scroll); } 
+1
source
0
source

Source: https://habr.com/ru/post/1303556/


All Articles