Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextField, auto-scroll when something is added  RSS feed

 
Arnie Fitz
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to make my JTextField, in a JScrollPane, scroll down to the bottom line whenever a line is added to the field. I'm making a simple chat application, and it's rather annoying to have to scroll manually every time a new message is added. Any help available?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arnie Fitz:
I'm trying to make my JTextField, in a JScrollPane, scroll down to the bottom line whenever a line is added to the field. I'm making a simple chat application, and it's rather annoying to have to scroll manually every time a new message is added. Any help available?


Did you mean JTextArea instead of JTextField?
Either way you can use the
 
Brian Cole
Author
Ranch Hand
Posts: 949
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Maneesh Godbole:


Either way you can use the


Why the trim()? It could do unexpected things if there's a lot of whitespace at the beginning of the document.

I might prefer setCaretPosition(textArea.getDocument().getLength()), as there's no reason to convert the Document's text into String form.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Brian Cole:


Why the trim()? It could do unexpected things if there's a lot of whitespace at the beginning of the document.

I might prefer setCaretPosition(textArea.getDocument().getLength()), as there's no reason to convert the Document's text into String form.


My bad. Currently I am working on something where I always need to call trim() so that it has become a habit!
Yes. Asking the document for the length is another (better) way of doing it.
 
Arnie Fitz
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, that worked great!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!