• Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextArea Scroll Focus  RSS feed

 
John Calson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello:

In short: How do you get focus on a scrolling JTextArea when a separate thread is appending text to the text area? Appending text is thread safe, no problem there, scrolling works fine but focus is NOT maintained on the current appended text line. Have tried requestFocus. Tried SwingUtilities.invokeLater. Does not solve problem. Any ideas?

Situation:
Created a class with a JFrame with a vertical scrolling JTextArea and a JButton. This class also creates a new thread. When the button is clicked, the new thread is started. The thread is a simulation that prints out data to the JTextArea using text.append(), which is thread safe. This works fine and vertical scroll bar appears when text exceeds vertical height, BUT focus is lost. Focus does not stay with the current output text line? I have tried requestFocus but did not help. Then tried to invokeLater the requestFocus, but that did not do it either.

Any help appreciated!
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
after your textArea.append()

textArea.setCaretPosition(textArea.getDocument().getLength());
 
John Calson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks much!
This works great.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!