Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Disappearing Text within a JTextField

  1. #1
    t338327 is offline Member
    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

    Default Disappearing Text within a JTextField

    Hello.
    I have a JTextField with a string inside it, which I want to disappear when the user clicks the JTextField so that the user doesn't have to delete the string I placed to have valid input.
    How would I do this?

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: Disappearing Text within a JTextField

    Use an appropriate listener (MouseListener, FocusListener, etc...) to listen for interaction with the JTextField, and set the JTextField contents to an empty String when the appropriate event is fired.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Disappearing Text within a JTextField

    But take care doing this. Consider that if you use a FocusListener, and have the JTextField's text deleted on focusGained, your poor user will have all his work deleted if all he wants to do is re-visit the JTextField and edit is text. Consider using a FocusListener and instead of deleting the text, selecting all (selectAll()) of the text. This way the user can type over the previous text if he doesn't want it, or can save the previous text if desired as well.
    doWhile likes this.

Similar Threads

  1. change the text of a JTextField
    By HelloWorld1234 in forum New To Java
    Replies: 14
    Last Post: 06-16-2012, 10:20 PM
  2. JTextField Disappearing
    By DinnerFork in forum AWT / Swing
    Replies: 22
    Last Post: 02-13-2012, 03:31 AM
  3. Why is javaw.exe Randomly Disappearing
    By Land Of Java in forum JavaFX
    Replies: 0
    Last Post: 10-05-2011, 08:43 PM
  4. Remember the text in JTextField
    By seredi in forum New To Java
    Replies: 11
    Last Post: 09-21-2011, 06:55 PM
  5. Reaplacing text in JTextField
    By PhQ in forum New To Java
    Replies: 5
    Last Post: 04-14-2010, 12:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •