• Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextField how to remove text when user clicks on text field.  RSS feed

 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a textField in which I have text in. The text is enter customer name. I would like for this text to disappear whenever the user clicks on the textField to enter a customer name. I am using the automated swing gui builder in NetBeans. Is their some sort of feature for this under properties of the text field? If not then what are my other options?
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's look at that a bit more carefully...

Suppose the user decides to type the customer's name, then after typing most of it they notice they left out a T near the beginning. So they click at the place where they want to insert the T, and everything they typed disappears. Would it be fair to assume that wasn't what you had in mind?

I'm going to assume that you didn't mean that, and that you only want the field to be cleared when the text field gets the focus. The way to do that is to attach a FocusListener to the field; here's a tutorial: How to Write a Focus Listener. As for how to do that with the Netbeans GUI builder, sorry, I have no idea.
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the Text Prompt class. It allows you to define a prompt to display for the text field without playing with the text.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I remembered I've seen this implemented before. But before I could google it up, the author itself has posted it! Thanks, Rob, and have a cow.
 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:Check out the Text Prompt class. It allows you to define a prompt to display for the text field without playing with the text.


Yes sir that is exactly what I wanted.....

Thank You
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!