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

[434 byte] By [AnanSmritia] at [2007-11-27 9:31:22]
# 1
textArea.setCaretPosition(textArea.getDocument().getLength());
Michael_Dunna at 2007-7-12 22:46:04 > top of Java-index,Desktop,Core GUI APIs...
# 2
I'm fairly sure if you just set the caret position of the text pane to the last index of the text you have inserted it will automatically scroll to the relevant point...jTextPane.setCaretPosition(jTextPane.getText().trim().length()-1);jTextPane.requestFocusInWindow();
c0demonk3ya at 2007-7-12 22:46:04 > top of Java-index,Desktop,Core GUI APIs...
# 3
Hi Dunn & c0demonk3y,Thanks for your immediate replies and help. your answers were very useful.
AnanSmritia at 2007-7-12 22:46:04 > top of Java-index,Desktop,Core GUI APIs...
# 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

AnanSmritia at 2007-7-12 22:46:04 > top of Java-index,Desktop,Core GUI APIs...
# 5
jTextPane.setCaretPosition(0);jTextPane.requestFocusInWindow();
Aniruddha-Herea at 2007-7-12 22:46:04 > top of Java-index,Desktop,Core GUI APIs...
# 6
Hi i thank all of you for your great help. I solved this problem. I did the followingsetCaretPosition(JTextArea.getDocument.getLength - length of the text + 250.)it works fine thanks again for your help
AnanSmritia at 2007-7-12 22:46:04 > top of Java-index,Desktop,Core GUI APIs...
# 7
> setCaretPosition(JTextArea.getDocument.getLength - length of the text + 250.)very dicky.int caretPos = textArea.getDocument().getLength();add texttextArea.setCaretPosition(caretPos);
Michael_Dunna at 2007-7-12 22:46:04 > top of Java-index,Desktop,Core GUI APIs...
# 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?

AnanSmritia at 2007-7-12 22:46:04 > top of Java-index,Desktop,Core GUI APIs...
# 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());

Michael_Dunna at 2007-7-12 22:46:04 > top of Java-index,Desktop,Core GUI APIs...
# 10
hi!please excuse me if i have mistaken..jTextPane.setCaretPosition(0);is it wrong.. will it not solve the purpose? why to do so many things?
Aniruddha-Herea at 2007-7-12 22:46:04 > top of Java-index,Desktop,Core GUI APIs...
# 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.

Michael_Dunna at 2007-7-12 22:46:04 > top of Java-index,Desktop,Core GUI APIs...
# 12
ok, i get it, thanx
Aniruddha-Herea at 2007-7-12 22:46:04 > top of Java-index,Desktop,Core GUI APIs...
# 13
Hi Dunn & Aniruddh,Thanks for your help. I solved that problem. I just followed Dunn's suggestion. It works.Thanks once again
AnanSmritia at 2007-7-12 22:46:04 > top of Java-index,Desktop,Core GUI APIs...