How to move the thumb of the scrollbar in Swing?
Hi,
I have a text area , scrollpane and button in a frame. Some texts will be written into the text area when I click the button. I want to move the thumb of the scrollbar to the bottom of the text area when I click the button for the second or third time. i.e thumb will point to the latest added text.
Can any one give me a pointer?
PS: I am using swing not awt components.
thanks in advance
sat
# 4
Hi,
Is there anyway to move the thumb to the beginning of the text instead of bottom?
for example, first time I am adding 4 lines of text after clicking ok button
second time i am adding the same 4 lines of text but thumb is pointing to the end of the line. But I want to point the beginning of the first line.
thanks again
# 8
Hi dunn,
Yes you are correct. But there was no way. I tried your code but it does not give me a desired result.
I have two option buttons. when I select one option button and click the Ok button. One set of fixed text is displayed on the text area. For instance the length of the text area is 433.
When i select second option button and click the oK button. Another set of fixed text is displayed and the length is approximately 255.
I am using this line for both
setCaretPosition(JTextArea.getDocument.getLength - length of the text + 250.)
Is there anyway to move the thumb to the starting line of the text each time user clicks the ok button?
# 9
> Is there anyway to move the thumb to the starting line of the text each time user clicks the ok button?
Element root = textArea.getDocument().getDefaultRootElement();
textArea.setCaretPosition(root.getElement(root.getElementIndex(textArea.getCaretPosition())).getStartOffset());
# 11
from the OP's original post
"i.e thumb will point to the latest added text."
I'm reading it that the textArea already has some text, then new text is appended. His
JTextArea.getDocument.getLength - length of the text + 250
takes him to somewhere on the first line on the newly added text, now he wants
to set the caret position at the beginning of that line.