Scrolling JTextArea

Hello,I have a problem with scrolling inside of JTextArea. I get position by method getVisibleRect()And then I set position by scrollRectToVisible()But it moves me to the end of textarea.How do I need to deal with it?
[259 byte] By [Guruslana] at [2007-11-26 14:02:40]
# 1

Its probably because you are adding text to the text area at the same time as you are trying to scroll. The setting of the caret postion when the text is added overrides the scrollRectToVisible. So try wrapping the scrollRectToVisible in a SwingUtilities.invokeLater() so it get added to the end of the event Thread.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.

camickra at 2007-7-8 1:45:30 > top of Java-index,Desktop,Core GUI APIs...
# 2

Could you please tell me more about SwingUtilities.invokeLater(). How and where do I need to use it?

...

//from initialization

jTextArea1.setColumns(20);

jTextArea1.setEditable(false);

jTextArea1.setLineWrap(true);

jTextArea1.setRows(10);

jScrollPane1.setViewportView(jTextArea1);

...

...

//this is method is launched when "start" button is pressed

private void startTimer() {

...

jTextArea1.setText(SessionBean.getText());

Rectangle rect = SessionBean.getLastRect();

jScrollPane1.scrollRectToVisible(rect);

...

}

}

...

...

//this is method is launched when "stop" button is pressed

private void stopTimer() {

...

jTextArea1.setText("");

...

}

}

...

...

//this is how I change rectangle, executed by Timer every 5 sec

public void setLastRect() {

SessionBean.setLastRect(jTextArea1.getVisibleRect());

}

Guruslana at 2007-7-8 1:45:30 > top of Java-index,Desktop,Core GUI APIs...