• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to create a ticker affect for text?  RSS feed

 
Bob Zoloman
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a program where you click a button and it adds a character to a JTextField. You can keep clicking on the button and it will add random characters to it.

What I am trying to do is when a new character is inserted, I want it to be inserted from the left side not added on to the right. Here's an example

Say I have the following text in a JTextField:
a b c d

and I want to insert 'e'. By default 'e' will be added so it looks like this
a b c d e

I want it to look like this
e a b c d

where the text is inserted from the left hand side. I have tried setting the caretposition for that JTextField to zero each time a click event takes place, but that didn't work. Any help is appreciated.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
textField.setText(randomChar + textField.getText());
 
Bob Zoloman
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That works perfectly, thanks!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!